Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Chapter 10 Self Help Question 9

 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ikpefua, thanks for the explanation. Much clearer now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic