wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Path Vs ClassPath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Path Vs ClassPath" Watch "Path Vs ClassPath" New topic
Author

Path Vs ClassPath

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
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


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 16, 2005
Posts: 14428
    
  23

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;

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 40034
    
  28
I think Quicktime is the commonest reason for having to add .; at the start of the classpath.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11496
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16
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

Joined: Sep 23, 2010
Posts: 422
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

Joined: Jul 21, 2011
Posts: 177
    
    6

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.


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 09, 2010
Posts: 15
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Path Vs ClassPath