aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Chapter 10 Self Help Question 9 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 Chapter 10 Self Help Question 9" Watch "K&B Chapter 10 Self Help Question 9" New topic
Author

K&B Chapter 10 Self Help Question 9

Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
Given the following directory structure





And given the contents of the java files



In the test directory:



In the myApp directory:



If the current directory is x, which invocation will produce the output "test/Baz"?

The book states option B is wrong:

B. java -classpath test FindBaz

I would like to know why the option B is wrong? Does the location of FindBaz must be defined in classpath (as a '.' in this case)? I don't think we do since the program class is in the current directory.

Thanks in advance.
Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
Hmm it seems in the later questions, same problem is encountered, so I deduce that you have to define the directory of the program you are attempting to run in the classpath as well.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Faraz Kadri wrote:Given the following directory structure





And given the contents of the java files



In the test directory:



In the myApp directory:



If the current directory is x, which invocation will produce the output "test/Baz"?

The book states option B is wrong:

B. java -classpath test FindBaz

I would like to know why the option B is wrong? Does the location of FindBaz must be defined in classpath (as a '.' in this case)? I don't think we do since the program class is in the current directory.

Thanks in advance.

Faraz Kadri wrote:I would like to know why the option B is wrong? Does the location of FindBaz must be defined in classpath (as a '.' in this case)? I don't think we do since the program class is in the current directory


Ikpefua wrote: Hello Faraz, the reason why option B is wrong is explained in the K & B Book, go to chapter 10, page 798 READ CAREFULLY the first paragraph, it says quote: "A very common situation occurs in which java or javac complains that it can't find a class file, and yet you can see that the file is IN the current directory! When searching for class files, the java and javac commands DONT search the current directory by default. -YOU MUST TELL THEM TO SEARCH THERE-. The way to tell java or javac to search in the current directory is to add a dot (.) to the classpath" .

B. java -classpath test FindBaz // MUST add a dot "."

C. java -classpath .:test FindBaz

F. java -classpath test:test/myApp:. FindBaz

That is why option B is wrong and options C and F are correct.

The second paragraph has a caption that makes you understand this better and it says quote: "REMEMBER That we're talking about .class files,
When you're telling javac which .java file to compile, javac looks in the current directory by default".

Hint:
Take note of the difference between a .class file(already compiled) and a .java file(not yet compiled).
you will see that in the case of a .java file, you do NOT need to use a dot "." since javac looks in the current directory by default.

Where-as in the case of a.class file, you NEED to use a dot "." since java does NOT look in the current directory by default.

And one more thing, REMEMBER that for option B to be correct you MUST also use the colon ":" just as specified in option C, you know this is used in the command line to separate directories.
I hope this helps.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Shaikh Ali
Ranch Hand

Joined: Jan 26, 2011
Posts: 51
Ikpefua, thanks for the explanation. Much clearer now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Chapter 10 Self Help Question 9