Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Problem with CLASSPATH environment variable.

  1. #1
    rjschilling is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Smile Problem with CLASSPATH environment variable.

    I've been trying to understand packages.
    I'm working from a shell under Ubuntu OS.
    The command

    java -classpath /home/rjs/Documents/Java/C09PackagesInterfaces/ MyPack.PackageDemo

    works as it should from anywhere in my filesystem. MyPack is the name of a package and
    its corresponding folder and PackageDemo.java is a source file whose first line is: package MyPack;

    Next I set
    CLASSPATH=/home/rjs/Documents/Java/C09PackagesInterfaces/
    and I check that
    java -classpath $CLASSPATH MyPack.PackageDemo
    works as it should from anywhere in my filesystem.
    According to my understanding, with CLASSPATH set as above,
    java MyPack.PackageDemo
    should work from anywhere in my filesystem but instead I get
    Error: Could not find or load main class MyPack.PackageDemo
    What am I doing wrong?

    Thanks and regards - Randy

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Problem with CLASSPATH environment variable.

    I'm not familiar with Ubuntu, but in Windows a classpath is dependent on your current directory. I have a file system like so: C:\Programming\Java\bin\

    I store my packages with class files in the bin folder. If my current directory is "C:\Programming\Java\", and I want to run a class file that is located in "bin", then I need to specify "bin" as my classpath so that the file can be found. This works because "bin" is inside my current directory.

    However, if my current directory is "C:\Programming\", and I want to run a class file located in bin, then I need to specify "Java\bin" as my classpath because there is no "bin" folder inside my current directory. I cannot use "bin" as my classpath anywhere on the system because the classpath is relative to the current directory.

    What I can do is specify "C:\Programming\Java\bin" as my classpath, and this would allow me to run a class file from anywhere.

    I'm assuming that the full path of the place where you store packages is: "C:\home\rjs\Documents\Java\C09PackagesInterfaces\ ". If you set your classpath variable to that full path, then you should be able to run your class files from anywhere. However, many people including Oracle themselves suggest that you simply use the "-classpath" command instead of setting up the environment variable.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with CLASSPATH environment variable.

    Quote Originally Posted by awinston View Post
    I'm not familiar with Ubuntu, but in Windows a classpath is dependent on your current directory.
    That is just a mystification of the matter, i.e. a classpath component doesn't act diferently than a simple, say 'ls bin' (or 'dir bin'); if you pass a relative path, the component is searched for starting from the CWD (Current Working Directory); if you pass a path containing a root component (as in 'ls /bin' or 'dir c:/bin') the path starting from the root is searched for. Classpath components don't act differently, neither in Unix/Linux or any Windows machine.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Problem with CLASSPATH environment variable.

    Quote Originally Posted by rjschilling View Post
    According to my understanding, with CLASSPATH set as above,
    java MyPack.PackageDemo
    should work from anywhere in my filesystem but instead I get
    Error: Could not find or load main class MyPack.PackageDemo
    What am I doing wrong?
    You are not doing anything wrong. But you will want to supply a -classpath. Even if it is just -classpath . for good measure. Once you add a package to a program, the java program will take account of what directories and files are available in the current directory.

    You can avoid absolute paths like <root>/home/$USER/Java/MyPack/Package, by using relative paths like ../ or packaging your program in a jar file, where the contents (for the most part) do not change. With a Jar file, the program can be executed anywhere, without the need of using -classpath as long as your jar file was created properly and you only created one jar file.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  5. #5
    rjschilling is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Smile Re: Problem with CLASSPATH environment variable.

    Thanks, all of your posts were helpful.
    I'll use the -classpath approach as several of you, and Oracle, suggest.
    Thanks Kammce for letting me know that I didn't do anything wrong.
    Thanks again, many kind regards - Randy

Similar Threads

  1. Set an environment variable
    By tonnnys in forum New To Java
    Replies: 3
    Last Post: 03-13-2012, 04:04 PM
  2. load environment variable before application run
    By anchitanc in forum New To Java
    Replies: 4
    Last Post: 01-02-2012, 05:51 PM
  3. Environment variable shows different from editable value
    By projecttoday in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 12:47 AM
  4. Classpath variable
    By shailenderanshuman in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 11:29 AM
  5. CLASSPATH Environment Variabl
    By fogus in forum New To Java
    Replies: 3
    Last Post: 12-18-2008, 08:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •