• 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

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Faz Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic