This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question related to Classpath when using the java and javac commands. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question related to Classpath when using the java and javac commands." Watch "Question related to Classpath when using the java and javac commands." New topic
Author

Question related to Classpath when using the java and javac commands.

Faz Ali
Greenhorn

Joined: Dec 02, 2010
Posts: 21
Hi All,

I'm just reviewing chapter 10 of the SCJP Java 6 study guide by Kathy Sierra and Bert Bates in which a paragraph highlights:

A very common situation occurs in which java or javac complains that it can't find a class file, and yet you see that the file is IN the current directory! When searching for class files, the java and javac commands don't search the current directory by default. You must tell them to search there.


Now I tested this, using the following simple classes, both of which are in the same folder (C:\Java Code)



browing to this directory in CMD and then using the command 'javac Student.java && java Student' both classes compiled fine and class Student ran. - Why hasn't it complained that it can't find the Book class?

I have a CLASSPATH environment variable but it is set to the following C:\Program Files\QuickTime\QTSystem\QTJava.zip;
Since I don't have a . present in the classpath environment variable and I haven't used the -classpath or -cp option when compiling and running the Student class, how is it that java and javac were able to find the Book class?
To test this more thoroughly, the next thing I did was delete the CLASSPATH environment variable altogether and retried the java and javac commands which again worked fine.
Lastly, I recreated the CLASSPATH environment variable and set it to C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files i.e. adding another directory but not onewhere my source files are. - Compilation and running of class Student worked this time also.
Any explanation would be welcome as the only thing I can think of at the moment is that this paragraph is erroneous? - It seems that these commands always look at the current directory by default irrespective of what is set in the CLASSPATH environment variable. The only time they don't is when you explicitly set a classpath using the java and javac commands via the -classpath/-cp options? I.e. something alongs the lines of the following:

C:\Java Code>javac -cp C:\Program Files Student.java
error: Class names, 'Files', are only accepted if annotation processing is expli
citly requested
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
3 errors[/code]


If someone could clarify the above, I would be very grateful.
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 40
For whatever reason your CP variable is been ignored and the default value of the current directory is been used instead. What echo do you get when you enter "set CLASSPATH" in your command line?

C:\Java Code>javac -cp C:\Program Files Student.java

I believe you will need to put quotes around "C:\Program Files" for that to run correctly.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

You have directory names with spaces in them. Make sure that you enclose those names in double quotes on the command line, for example:

java -cp "C:\Java Code\classes" MyProgram

Without the quotes, the command prompt will interpret the parts separated by spaces as separate elements on the command line.

Note that adding the directory C:\Program Files to the classpath is strange. That's a directory that you are not supposed to put files in yourself, certainly not Java source or class files. Do not put your source or class files in the directory C:\Program Files.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Faz Ali
Greenhorn

Joined: Dec 02, 2010
Posts: 21
Ok, I've confirmed the CLASSPATH environment variable is set to C:\Program Files\QuickTime\QTSystem\QTJava.zip;
I've confirmed this by using "set CLASSPATH" in CMD which prints the following:


C:\Users\FaZ>set CLASSPATH
CLASSPATH=C:\Program Files\QuickTime\QTSystem\QTJava.zip;

C:\Users\FaZ>


For testing purposes, I've put both the Book and Student .java files in the same directory at the path C:\Users\FaZ\Desktop\Code. I've since browsed to this directory via CMD and then run the following command which works absolutely fine:


Regarding putting code into C:\Program Files, this was just for quick testing purposes. I absolutely do not put code in there, I just used that directory with this specific example.

The next thing I've tried is the following, which I expected to fail since I am explicity defining a classpath in my javac invocation which does not contain the current directory:

C:\Users\FaZ\Desktop\Code>javac -cp "C:\Program Files" Student.java
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
Student.java:5: cannot find symbol
symbol : class Book
location: class Student
Book book = new Book();
^
2 errors

C:\Users\FaZ\Desktop\Code>


Since I can confirm that I have a CLASSPATH environment variable which is set to something, can I also then confirm that the books statements about java and javac not looking in the current directory is erroneous? - It seems the only time it doesn't look in the current directory is when a classpath is defined in the command line. I'm running Windows 7 with Java version "1.6.0_24" - Java(TM) SE Runtime Environment (build 1.6.0_24-b07) - Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing) if that makes a difference?
 
wood burning stoves
 
subject: Question related to Classpath when using the java and javac commands.