*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 5 Study Guide: Self Test Q. 12 - Page 788 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP 5 Study Guide: Self Test Q. 12 - Page 788" Watch "SCJP 5 Study Guide: Self Test Q. 12 - Page 788" New topic
Author

SCJP 5 Study Guide: Self Test Q. 12 - Page 788

Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Dear Ranchers,

Here is my question; would appreciate your replies......

Source: SCJP 5 Study Guide: Self Test Q. 12 - Page 788


Correct Answer as per the book: H

Why does the code require reference to the current directory (i.e. :.) when launching class UseKit from the command-line using java command when the same is NOT required while compiling?

I believe we have to specify reference to the current directory in classpath using java and javac commands when we have to search for OTHER CLASSES (dependencies) to complete the operation. Pls. correct me if I'm wrong.

I compile and run classes (classes that don't require access to other Non-standard J2SE classes) without refering to the current directory in the classpath. I have removed classpath settings (including .) from my system's environment variables.


Ravinder
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Ravinder,


Why does the code require reference to the current directory (i.e. :.) when launching class UseKit from the command-line using java command when the same is NOT required while compiling?


I think you are confusing the -classpath parameter, which is used to find .class files instead of .java files.

The -classpath parameter has nothing to do with .java files. It's used to find .class files only.

When compiling a .java file, in fact javac.exe does not require you to inform the .java file directory if you are running the javac.exe from the same directory as the .java file.


I believe we have to specify reference to the current directory in classpath using java and javac commands when we have to search for OTHER CLASSES (dependencies) to complete the operation. Pls. correct me if I'm wrong.


It depends...

For example, supposing you have inside c:\temp the .class file for the following class :



If you run from within c:\temp directory the command java HelloWord, no problem. java.exe will be able to find your class.

However, for the code you post above, you overwrite the -classpath with the .jar file, so you ALSO need to inform the current diretory in order to allow java.exe find your main class.

Please let me know if I can do anything else to help you with.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Thanks Edisandro...I got it now!

The -classpath parameter has nothing to do with .java files. It's used to find .class files only.


..is correct. However with respect to my following statement:

I believe we have to specify reference to the current directory in classpath using java and javac commands when we have to search for OTHER CLASSES (dependencies) to complete the operation. Pls. correct me if I'm wrong.


I would like to further add that: you need to specify reference to the current directory using the classpath parameter when you have to invoke your compiled class(es)located in your current directory. For example: lets say you have a class called JavaWorld in c:\temp directory -



It depends...

For example, supposing you have inside c:\temp the .class file for the following class :


code:
--------------------------------------------------------------------------------

class HelloWord{ public static void main(String[] args){ System.out.println("Hello Word"); }}

--------------------------------------------------------------------------------


If you run from within c:\temp directory the command java HelloWord, no problem. java.exe will be able to find your class.



For the HelloWord class - the code won't run (after compiling) just by saying:


..for the reason mentioned above.

Thnx
Ravinder
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
oops...a small change in the JavaWorld class code mentioned earlier. Rest everything remains same.



-----------
Ravinder
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Ravinder,

I think you are a little bit wrong concerning running your class from within the same directory.

Let me explain why :

First off, I mistype the class name : I wrote HelloWord instead of HelloWorld.

Well,

If you have the following class inside the c:\temp directory.


You can compile and run in two ways from within c:\temp directory :

1st.
javac HelloWorld.java
java HelloWorld

2nd.
javac HelloWorld.java
java -classpath . HelloWorld

But if you class imports any other classes which you have to inform in classpath, compulsorily you have to inform the . in your classpath in order to allow java.exe find your main class.

It's the case of your first example. Because of you need to inform the -classpath com/KitJar.jar, you ALSO DO need to inform the . otherwise, javac will not be able to find your main class.


class JavaWorld {} You will compile and run this class using following commands from the current directory: c:\temp>javac JavaWorld.javafollowed by...c:\temp>java -classpath . JavaWorld It won't and shouldn't run (atleast on my machine) using: c:\temp>java JavaWorld It throws an exception saying "Exception in thread "main" Java.lang.NoClassDefFoundError: JavaWord"


How can you see a "Exception in thread "main" java.lang.NoClassDefFoundError: JavaWord" message if your command line is java JavaWorld ?
[ May 13, 2006: Message edited by: Edisandro Bessa ]
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Hi Edisandro,

You can compile and run in two ways from within c:\temp directory :

1st.
javac HelloWorld.java
java HelloWorld

2nd.
javac HelloWorld.java
java -classpath . HelloWorld


I couldn't run the class using the 1st way as you've mentioned above...which I believe is ok because the java interpreter (java) needs classpath for HelloWorld class file even running from within c:\temp directory.

Have you removed reference to the current directory (.; or . in classpath settings in your os environment varible settings?

But if your class imports any other classes which you have to inform in classpath, compulsorily you have to inform the . in your classpath in order to allow java.exe find your main class.


This is correct. java.exe needs to know the classpath of all (excluding standard J2SE) the compiled classes that are imported into the main class.

How can you see a "Exception in thread "main" java.lang.NoClassDefFoundError: JavaWord" message if your command line is java JavaWorld ?


Yes...I get this exception if I run HelloWorld class from command line from c:\temp directory without specifying "-classpath ." variable.
It is because the java interpreter needs to be intructed explicitly (by way of -classpath .) to look for class file(s) in the current directory.

Hope this clarifies...and do let me know if i'm wrong anywhere.

------------
Ravinder
praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
Hi Ravinder,
praveen vempati
Greenhorn

Joined: May 15, 2006
Posts: 10
Hi Ravinder,
Sorry for the previous post. The class path in my system is empty. I have placed HelloWorld.java in D:\temp. I was able to compile as well as run HelloWorld from temp folder. I guess java.exe checks the cuurrent folder by default if the class path is not specified at run time. Please correct me if I am wrong
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
Hi Praveen,

Going by the presumtion that you have mentioned the correct answer to Q.12 as stated above in the very first post should be option F and not H..which in reality is not the case.

-----------
Ravinder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP 5 Study Guide: Self Test Q. 12 - Page 788
 
Similar Threads
Development
Source: SCJP 5 Study Guide: Self Test Q. 12 - Page 798
K & B Book Chapter 10 Question 12
classpath for jar file K&B
Classpath question from K&B