This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
The compiler looks for imported classes in any directory or jar listed in your classpath. The examples you gave ship with the JDK so you don't have to add anything to the classpath for those. If you import from other packages, say an open source project you download or even your own projects, you may have to add jars and directories to the classpath.
Classpaths seem to confuse nearly everybody at first. Feel free to post back if that opened more questions than it answered.
As a style point, many people prefer to import individual classes rather than using the * star. It's a little more work but provides good documentation of just what classes you are using and help avoid surprises where two imports might give ambiguous paths to the same classname.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James: It's a little more work but provides good documentation of just what classes you are using and help avoid surprises where two imports might give ambiguous paths to the same classname.
That was exactly the problem I had recently. Two different packages - same class names imported. It took me a long time to realise that Java didn't know which class to use. In the end, I used the actual path of the class from the JDk in the code itself, which makes it easier to read if you are using it only once in a long piece of code.
Joined: Feb 03, 2005
Thanks for your reply,
But I am still confused with the term "CLASSPATH". I mean i have set my path to "c:\jdk1.3\bin". But the i have set my classpath to that particular folder where i have saved my .java file...
If u r saying that compiler will look out the .jar in you classpath but there isn't any .jar file in my classpath..
I am agree with your answer when i am working with servlet that if i want to compile servlet then i have to specify the classpath of servlet.jar..
But if it is a normal .java program then what is the need of classpath...
Don't get PATH and CLASSPATH confused. The PATH environment variable is a list of directories (or folders) that the operating systen searches when it is instructed to execute a program.
The CLASSPATH is list of folders that Java uses to search for classes that it may need to load when running a program. In your case, you say that your .java file is in a folder that is specified on your classpath - that's fine. If you used the default settings when compiling your .java file, then your .class file will also reside in this same folder. If you run your program with your current directory set to this folder, your program should run fine.
Now, change your current directory to something different and try and run your program - it should still run as Java will look for your class and should find it in the folder you specified in your classpath.
Regards, JD [ March 17, 2005: Message edited by: John Dell'Oso ]