• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Path Vs ClassPath

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting confused between PATH and CLASSPATH. Here is my understanding

The PATH variable contains directories where binary files are located [ C:\Program Files\Java\jdk1.6.0_07\bin; ]
The CLASSPATH contains directories (or JAR files) [ C:\Program Files\Java\jdk1.6.0_07\lib; ]

I am able compile & run the programs without CLASSPATH when I am in some directory like "C:\CoreJava". I have Hello World.java in "C:\CoreJava".

C:\CoreJava>javac Helloworld.java
C:\CoreJava>java HelloWorld

So, Why should I use CLASSPATH eventhough I am able to run programs ?

Thanks,
Harikrishna
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:Hi,

I am getting confused between PATH and CLASSPATH. Here is my understanding

The PATH variable contains directories where binary files are located [ C:\Program Files\Java\jdk1.6.0_07\bin; ]


Correct. And it is used by your OS and command shells to find executable programs. So that, for example, when you type java on the command line, each element of the PATH will be searched for an executable named java (or, on Windows, also java.exe or java.bat).

The CLASSPATH contains directories (or JAR files) [ C:\Program Files\Java\jdk1.6.0_07\lib; ]


Correct. And it is used by Java's classloaders to find class files.

So, Why should I use CLASSPATH eventhough I am able to run programs ?


You should not use the CLASSPATH environment variable. What you should use is the -cp or -classpath command line argument.

When no classpath is specified at all, java defaults to using the current directory ( . ) as the classpath.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:The CLASSPATH contains directories (or JAR files) [ C:\Program Files\Java\jdk1.6.0_07\lib; ]

You should not set the CLASSPATH environment variable to C:\Program Files\Java\jdk1.6.0_07\lib;
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As JV said, you should not set a system CLASSPATH at all. If it is already set, add .; to its beginning. If it isn’t already set, creating a new CLASSPATH usually does more harm than good.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As JV said, you should not set a system CLASSPATH at all. If it is already set, add .; to its beginning. If it isn’t already set, creating a new CLASSPATH usually does more harm than good.


And if you install QuickTime, it will set it for you, which is horribly annoying and leads to all kinds of confusion.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Quicktime is the commonest reason for having to add .; at the start of the classpath.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PATH is used by the OS to find executables to run.

CLASSPATH is used by the JVM to find class files to run.

So, the OS uses the PATH variable to find the java executable, which then in turn uses the CLASSPATH to find the class files to run.

Note that you don't actually have to set either one. They are both a convenience that allow you to type less.
 
Rod Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CLASSPATH is used for the entire system; In other words, all Java applications on that machine -- use the CLASSPATH. On the other hand, if there is a need to override -- the CLASSPATH -- then -cp (or -classpath) option is used. Moreover, The CLASSPATH option can be added along with -cp option as well.

Using -cp or -classpath option is a preferred option.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to see an example work with classpath and example doesn't work without classpath. Can anyone provide couple of examples for this. Thank you for your help !!
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have a Java app that relies on an external jar 'ext-lib.jar'. Or for a real-world, JDBC example, a database-connected application...'ojdbc6.jar'.
Say for example, you have compiled your app into
/myapp/SomeApplication.jar
and your external libraries are in
/myapp/lib/ext-lib.jar
/myapp/lib/ojdbc6.jar

In this case, if you cd into /myapp and run

it will fail as soon as you get to some code that uses one of those libraries, because the JVM doesn't know to look in the lib folder.

But if you run

it will work fine because you've explicitly specified the location of your library files via the classpath so the JVM looks in the lib folder and finds them.

I'm using the -cp option for setting CLASSPATH only for the given app because these settings are specific to this example, certainly not something I'd want to apply to all Java apps on my computer.

(Note Linux separates classpath args with a : rather than a ; )

Example assumes that your SomeApplication.jar file includes a main-class definition in the manifest file. Otherwise specify your.class.Name as the last argument, which is the startup class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harikrishna Gorrepati wrote:I want to see an example work with classpath and example doesn't work without classpath. Can anyone provide couple of examples for this. Thank you for your help !!


A better approach would be for you to try to construct those examples yourself, based on the explanations you've been given so far, and if you have problems or questions, post what you tried and ask a specific question about the parts you didn't get.

Good luck!
 
Oliver Chase
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:PATH is used by the OS to find executables to run.

CLASSPATH is used by the JVM to find class files to run.

So, the OS uses the PATH variable to find the java executable, which then in turn uses the CLASSPATH to find the class files to run.

Note that you don't actually have to set either one. They are both a convenience that allow you to type less.


Thank you...this is the clearest explanation that I have seen yet. Concise yet complete.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic