• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classpath

 
bala kan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15481
43
Android IntelliJ IDE Java Scala Spring
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

/home/hci/gnu/bin/grep

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic