Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By kjkrum

Thread: In Linux export PATH doesn't work but the giving classpath to javac works. Why?

  1. #1
    srikanthradix is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Question In Linux export PATH doesn't work but the giving classpath to javac works. Why?

    In Linux,

    Why doesn't export PATH=path_to_lib doesn't work and -classpath works?

    lib has all the jar files.

    I exported the path to the lib folder.
    export PATH=$PATH:/opt/lib

    I also did export CLASSPATH=$CLASSPATH:$PATH

    Then did a javac,

    javac clazz.java

    It is giving compilation problems.

    Whereas, when we did javac --classpath /opt/lib/something.jar clazz works fine. Why is that?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,257
    Rep Power
    25

    Default

    What do the linux experts think about this problem?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Setting your CLASSPATH to the directory containing your jars isn't enough. java/javac won't automatically look inside all the jars in that directory.

    Before I started using Eclipse, I had something like this in my .bashrc (or .bash_profile, I can't remember):

    Java Code:
    CLASSPATH="."
    for fname in /home/kevin/lib/java/*.jar; do "CLASSPATH=$CLASSPATH:$fname"; done
    export CLASSPATH
    Last edited by kjkrum; 06-02-2011 at 01:13 AM. Reason: added double quotes
    Get in the habit of using standard Java naming conventions!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Also, out of curiosity, I found this: unix - Setting multiple jars in java classpath - Stack Overflow

    Apparently Sun added wildcards to classpaths in Java 1.6. (Who knew?)
    srikanthradix likes this.
    Get in the habit of using standard Java naming conventions!

  5. #5
    srikanthradix is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    javac -classpath /home/lib/*.jar clazz.java seems to be working. But, I can't believe that PATH doesn't work.
    What is PATH then? I remember adding PATH to Windows environmental variables before running javac which worked before.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    PATH is a list of locations your shell searches for a command or program. It's unrelated to Java's classpath.

    Edit: Like if javac is located at /usr/bin/javac, and /usr/bin is in your PATH, then you can just type "javac" and the shell will find it. Classpaths are conceptually similar, but otherwise unrelated. They tell the JVM where to look for classes in the same way PATH tells the shell where to look for programs.
    Last edited by kjkrum; 06-02-2011 at 07:05 AM. Reason: clarification
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. How JVM works in linux box ?
    By vinothjava in forum Advanced Java
    Replies: 14
    Last Post: 05-06-2011, 12:02 PM
  2. Replies: 1
    Last Post: 11-03-2010, 08:49 AM
  3. classpath in linux...
    By miko5054 in forum Eclipse
    Replies: 3
    Last Post: 09-23-2010, 08:50 AM
  4. Replies: 0
    Last Post: 10-05-2009, 11:25 PM
  5. Replies: 3
    Last Post: 08-14-2009, 12:57 PM

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
  •