• 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

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

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops...a small change in the JavaWorld class code mentioned earlier. Rest everything remains same.



-----------
Ravinder
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravinder,
 
praveen vempati
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic