Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: SCJP 5 Study Guide: Self Test Q. 12 -


12. Given two files:





And the following sub-directory structure:
test
|--UseKit.class
|
com
|--KitJar.jar


If the current directory is test, and the file pkg/Kit.class is in KitJar.jar, which
command line will produce the output bc ? (Choose all that apply.)

A. java UseKit b c
B. java UseKit a b c
C. java -classpath com UseKit b c
D. java -classpath com:. UseKit b c
E. java -classpath com/KitJar.jar UseKit b c
F. java -classpath com/KitJar.jar UseKit a b c
G. java -classpath com/KitJar.jar:. UseKit b c
H. java -classpath com/KitJar.jar:. UseKit a b c



Correct Answer as per the book: H

Shouldn't this be F & H since the classpath to the current directory is not necesarry since you are not using other classes apart from the one you start up using the 'java UseKit' command? Hence UseKit does not need any classes in the current dir?


I've read on old discussion on this subject but it is still not clear what the right answer is and if the book just contains an error here or not.

[ August 07, 2007: Message edited by: Marc Wentink ]
[ August 07, 2007: Message edited by: Marc Wentink ]
 
christian combarel
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can't be answer F because you have to tell the JVM where to find the UseKit.class.
So, the current directory (.) must be in the classpath as it's not included by default.
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can't be answer F because you have to tell the JVM where to find the UseKit.class.


I think the JVM starts from the UseKit class, and so it does not have to find it. It only has to find stuff that should be included to the UseKit class, like the instructions in the jar file.
 
christian combarel
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just tried the test (on Linux platform + JDK6.0). It confirmed that the location of the UseKit.class must be given to the JVM (answer H only).
 
Marc Wentink
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. That surprises me since you can make something like a 'Hello.java' and make one class file, and call java Hello with no problems. So when does the java command need the dir of the class with main in it in the classpath?

You would say at the moment it needs a classpath to look for other stuff, it also needs the path to 'main.class' to get back again. While if you only have one file it doesn't bother to check the classpath for class files and it does not need anything?

You must admit it's a bit confusing.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Wentink:
Ok. That surprises me since you can make something like a 'Hello.java' and make one class file, and call java Hello with no problems. So when does the java command need the dir of the class with main in it in the classpath? ...

For convenience, the classpath defaults to the current directory. However, once you specify a classpath, then there is no longer a default. So if you are specifying a classpath and want to also retain the current directory, then you need to include it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic