• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PATH & CLASSPATH?

 
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the use of PATH & CLASSPATH in java & how to set them for Windows XP?i m begginer & running programs from commandline ....
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll tell you how it works.

On command prompt, when you type java, javac, javah or any other exe name, it's been looked up in folders you specify in Path variable.

For example, you have javac.exe file in C:/JDK/bin folder, you can run javac tool through command prompt by:
1. Go into that folder (bin) and type it's name (javac).
2. Add that folder path (C:/JDK/bin) into Path variable (through environment variable) and then type it's name (javac) from any location, on command prompt.

Classpath is specific to Java. Your classes are looked up in the folders you specify here e.g. you have a HelloWorld.class in d:/examples folder.

To run this class from command prompt:
1. Go into that folder (examples) and run it (java HelloWorld).
2. If you are not into that folder then set the classpath in java command e.g. java -classpath d:/examples HelloWorld

Hope it helps.
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PATH is for commands,
CLASSPATH is for classes.

what is the use of PATH & CLASSPATH in java


PATH: When ever you use java, javac, etc., commands the underlying operating system(for e.g. windows) should be able to get the path for those commands.
CLASSPATH:When ever you want to run an application(for e.g. executable java class, applet, servlet, etc) it may depend/call methods from other classes, then your operating system should be able to find those classes, those classes/jar files are specified with CLASSPATH.

how to set them for Windows XP?


Refer this link
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rayapudi Murali Krishna wrote: . . . Refer this link

That link is out of date. There are instructions in the installation page; if you use Windows try section 4.

Don't set a system CLASSPATH at all, unless you get problems.
 
nick kaushik
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i set the path variable to my bin folder,but it still shows error: cannot read :A.java (A is name of my class)
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which directory are you in? It is probably easier if you pass the command

mkdir java

or similar when you first open your command line; then you can get to your "java" directory (or whatever else you called it) quickly with the command

cd java

You need to be in the correct directory. Try the following commands
dir (DOS/windows)
ls (Unix/Mac/Linux)

then you can see whether your file A.java is actually in your current directory.
 
nick kaushik
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i start command line i m at c:\Document & settings\admin>
 
M K Rayapudi
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick kaushik wrote:how to set them for Windows XP?


follow the below steps:
My Computer (right click) --> properties (select the option) --> Advanced (select the tab)
--> Environment variables (click on this button)

Either in User Variables OR System Variables section create variables as follows:
Create PATH variable
--> click on "New" button
--> provide values as follows:
Variable Name: PATH
Variable Value: C:\Program Files\Java\jdk1.6.0_10\bin;.;

Create CLASSPATH variable
--> click on "New" button
--> provide values as follows:
Variable Name: CLASSPATH
Variable Value: C:\Program Files\Java\jdk1.6.0_10\jre\lib\rt.jar;


@ Kaushik
My suggestion to you is, If you have any friends who already started programming java, sit with them for a while
at least execute one simple java program with the help of your friend.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Disagree.

Don't set up a new CLASSPATH variable. You need a PATH, but setting a system CLASSPATH can do more harm than good.
The value suggested is out of date and you should not put that in your CLASSPATH if you are using a version of Java more recent than about 1.2.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why are you running a windows machine as "admin" rather than as "nick"?
 
M K Rayapudi
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Disagree.

Don't set up a new CLASSPATH variable. You need a PATH, but setting a system CLASSPATH can do more harm than good.
The value suggested is out of date and you should not put that in your CLASSPATH if you are using a version of Java more recent than about 1.2.



@Campbell Ritchie
Thank you for your suggestion.
actually I am not 100% sure about my previous post.
Currently I am using Eclipse IDE, earlier I use to work at command prompt, then I use to set CLASSPATH.
What is wrong with CLASSPATH, can you please clarify?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That CLASSPATH is unnecessary; if there is no CLASSPATH set, the the java tool will use the current directory as a default. If you set that CLASSPATH, then java will be unable to find work in the current directory.
It used to be necessary to include the Java installation directory in the CLASSPATH, but that has not been necessary for several years.
It may be necessary to modify the system CLASSPATH, but only if one has already been established. There are some applications (eg QuickTime, Oracle) which are notorious for altering CLASSPATH and PATH settings and causing bothr, but unless that has occurred, one is better off without an explicit CLASSPATH.
 
nick kaushik
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 more thing guys--- right now i m saving all my .java files in bin.....would that matter & now that i have set Path variable do i need to go any other directory or should i start command prompt & do javac File.java & then java File?
& also are Path & PATH same?cuz i already have a Path variable in System variables ?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick kaushik wrote:1 more thing guys--- right now i m saving all my .java files in bin.....would that matter


Is this the bin directory inside JDK then that's a very bad practise. You should have a separate directory for your source files.

... & now that i have set Path variable do i need to go any other directory or should i start command prompt & do javac File.java & then java File?


Yes. If the "Path" has set you should be able run any executables inside the bin (javac,java etc..) from anywhere in the command prompt.

... & also are Path & PATH same?cuz i already have a Path variable in System variables ?


I'm not sure about other OS's but in windows,Yes. You can test this from command prompt (echo %PATH% OR echo %Path% - this is in Vista)
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:
Is this the bin directory inside JDK then that's a very bad practise. You should have a separate directory for your source files.

Agree. I have already suggested how you can do that: mkdir java, as I posted yesterday.

.

...

... & also are Path & PATH same?cuz i already have a Path variable in System variables ?


I'm not sure about other OS's but in windows,Yes. You can test this from command prompt (echo %PATH% OR echo %Path% - this is in Vista)

I think Windows is case-insensitive, but Unix/Mac/Linux is case-sensitive, so it has to be PATH. Note the use of $PATH rather than %PATH%. Also note there were no error messages for non-existent environment variables.
On a Linux box:

campbell@queeg:~$ echo $PATH
/usr/java/jdk1.6.0_16/bin:.:/home/campbell/forth/rvm/rvm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
campbell@queeg:~$ echo $Path

campbell@queeg:~$ echo $CLASSPATH

campbell@queeg:~$ echo $classpath

campbell@queeg:~$

When I changed PATH to path in my .bashrc file, the OS would no longer recognise "java". So it has to be PATH on Linux.
 
nick kaushik
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OMG!!! guys i am throughly confused.....so Path means directory where javac is located.then how would the system know about where my .java files are located if they are in any other directory?
ok now tell me from a-z.i have Java in C:\Program Files\Java\jdk1.5.0_16\bin.Now what should be the Path,what should be directory for source files.& how to run & compile them.....?
& on the same note why doesnt it works from bin?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nick kaushik wrote:so Path means directory where javac is located.then how would the system know about where my .java files are located if they are in any other directory?


You have to give the path to your java source files when compiling. If you have java source file named Main.java in F:/Project/Java/ then you have to issue the following command (assuming you have set the Path variable correctly pointing to the bin directory of JDK, then doesn't matter where you are at the command prompt):
"javac F:/Project/Java/Main.java" OR "javac Main.java" (this is if you are in the F:/Project/Java directory, simply use the file name)
And there are other command line switches you can use when you get familiar with it later.

..& on the same note why doesnt it works from bin?


Who said it doesn't work . It works, but a very bad practise.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:

on the same note why doesnt it works from bin?

Who said it doesn't work . It works, but a very bad practise.

If you put your work into the bin directory, you risk confusion between the installed files and the files you put there. Even worse, if you delete a Java installation directory after upgrading to a new version, all your work will vanish too

It is better to navigate to the directory where you have put your .java files, then (if you haven't got packages) you can use the simple names of the files.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Test.java file on desktop compiles fine.
But java command fails and throws error.
Do I need to set the path of jre's bin in environment variable

C:\Documents and Settings\samareshg\Desktop>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samaresh Gupte wrote:My Test.java file on desktop compiles fine.
But java command fails and throws error.
Do I need to set the path of jre's bin in environment variable


No. Only JDK's bin is needed to set.


C:\Documents and Settings\samareshg\Desktop>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


This is because "java" cannot locate your compiled class (i.e: class bytecode). May be it's in a different location? Where is your compiled class located? And Welcome to JavaRanch
 
Samaresh Gupte
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for a prompt reply

my class file is located on the desktop itself.
So that makes it more confusing.

I also tried adding the the path of the test.class in CLASSPATH of environment variable but it doesn't work.
 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add dot "." which means current working directory to your classpath variable.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is in your system CLASSPATH? Did you set it up all by yourself?
You might be better deleting the CLASSPATH.

Don't put everything in the desktop; you will have hundreds of things on your desktop and they will vanish when you clear up your desktop. I have told you at least twice in this thread how to create a "java" folder.
 
Samaresh Gupte
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is in your system CLASSPATH? Did you set it up all by yourself?
You might be better deleting the CLASSPATH.

Don't put everything in the desktop; you will have hundreds of things on your desktop and they will vanish when you clear up your desktop. I have told you at least twice in this thread how to create a "java" folder.




Yes I set up the CLASSPATH by myself. I am aware that classpath can do more harm than doing good. So only necessary jar files have been addded viz.
Y:\WEB-INF\classes;C:\Program Files\Java\jdk1.5.0_09\lib\servlet.jar;C:\Program Files\Java\jdk1.5.0_09\lib\servlet-api.jar.

I think there is some problem with the classloader.

Should I try re-installing the JDK. Because sometimes the java command runs perfectly fine and sometimes it throws an error.

The same file , with the same environment variables runs perfectly fine on my friend's machine.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would probably do better to delete the entire CLASSPATH.
If you need servlets, you are better off adding them to the CLASSPATH with the -cp option when you run the application. If you add them to your CLASSPATH it will become cluttered and you won't know whihc entries are still required.
 
Samaresh Gupte
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You would probably do better to delete the entire CLASSPATH.
If you need servlets, you are better off adding them to the CLASSPATH with the -cp option when you run the application. If you add them to your CLASSPATH it will become cluttered and you won't know whihc entries are still required.



Thanks, deleting the classpath worked
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can find more details about the -cp flag for java here. I presume for your servlets you would use
java -cp C:\Program Files\Java\jdk1.5.0_09\lib\servlet.jar;C:\Program Files\Java\jdk1.5.0_09\lib\servlet-api.jar package_name.Foo
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic