File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath" Watch "classpath" New topic


bala kan
Ranch Hand

Joined: Aug 20, 2012
Posts: 44
what is meant by classpath in java.. why do we use and is there any difference between set path and classpath..
please explain me in detail

thanks in advance
RatiKanta pal
Ranch Hand

Joined: Nov 13, 2011
Posts: 83

While we are giving path,it means we are giving the path of jre for complie my source.
now when its done,it produce .class file which is the machine code for jvm so we give the class path to system32 as jvm resides inside that system 32.

experts correct me if i am wrong
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

path and classpath are two different things. as the name suggests classpath is class search path. this is the list of directories where your compiled .class files are located.

now path variable is the list of directories containing the executables. for example i have following commandline invokation java -classpath=c:/; HelloWorld

now for this to run successfully you need to tell commandline where is the location of java binary. for this you use path variable. you set path variable to your jdk bin directory. but remember path variable is not limited to java or javac binaries. for any executable you can use it.

after you have set path for jdk binaries , supppose that HelloWorld class file is located in say c drive and you are in d drive. now you have to tell java binary where YOUR class file(s) is/are located. for this we have -classpath switch. now java will search for c:/ directory and will find HelloWorld class file there and will run it
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46383
Agree. One more thing about classpaths: You will hear advice about how to set a system classpath, but the correct advice is: Don’t. Never set a classpath, because it will vary from application to application. You may need to alter an already‑existing classpath, but that is something different.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

See How to set the Classpath in the JavaRanch FAQ.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

the 'path' environment variable is use by the operating system (OS). When you type a command, like "dir", the OS searches every directory listed in the path for that executable/binary. Hopefully, it finds it. For example, I don't really want to type


every time I need to grep a file. And since there are a ton of programs in that /home/hci/gnu directory (chown, cp, du, make, mv...), I can put that directory in my path. So then I only have to type "cp ..." and Unix knows to look (among other places) in that directory. It finds 'cp' there, and then fires it up.

classpath is similar, but it is used by the JVM when trying to find .class files.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: classpath
It's not a secret anymore!