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.