permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K & B Book Chapter 10 Question 12 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K & B Book Chapter 10 Question 12" Watch "K & B Book Chapter 10 Question 12" New topic

K & B Book Chapter 10 Question 12

Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 50
Hi All,

the selftest question 12 of chapter 10 looks like this:

And the following sub-directory structure:

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

Answer given:
H is correct.
A, C, E, and G are incorrect if for no other reason than args[] is 0-based.
B, D, and F are incorrect because java needs a classpath that specifies two directories, one
for the class file (the . directory), and one for the JAR file (the com directory). Remember,
to find a JAR file, the classpath must include the name of the JAR file, not just its directory.

I have a question to the option F.
The answer F is wrong because the classpath doesn't contain the current directory (.) and the Java virtual machine (invocation java [options] class [args]) doesn't look in the current directory by default if it load a .class file. This explanation is understandable.

But why if the current directory is test an I have some class file there e.g. Foo.class and I make following invocation

java -classpath /dirOtherThanTest Foo

the Foo class is loaded and executed without problem? There is more if the Foo.class need a further class for the execution e.g. Bar.class, which resides in the same driectory test and the same invocation is used:

java -classpath /dirOtherThanTest Foo

it also works without problem? Can anyone explain why it work, the current directory isn't defined in the -classpath but java can find and execute the .class files anyway?

kind regards

SCJP 6.0 [95%] OCE EJBD 6.0 [93%]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
In the directory you list in the CLASSPATH, is there a class named Foo.class?
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 50
Hi Keith,

there is no CLASSPATH defined on my machine. But I found out some interesting thing. I have Windows as operating system and if I invoke java like this

java -classpath c: Foo

or without classpath flag at all like this

java Foo

it finds the Foo.class and executes it correctly when it exists in the current directory. But the invocation

java -classpath c:\ Foo

where Foo is in another dir as c:\ ends with a NoClassDefFoundError as expected...
I agree. Here's the link:
subject: K & B Book Chapter 10 Question 12
It's not a secret anymore!