Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Possible question eligible for errata. 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 "Possible question eligible for errata." Watch "Possible question eligible for errata." New topic
Author

Possible question eligible for errata.

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Guys,

I'd like to discuss with you the possibility to submit the below question to Kathy and Bert as a candidate for an errata.

Recently I opened a thread reporting this but I received only one concrete opinion about the question and such opinion also confirmed my supposition that the question would be eligible for an errata.

So, I'm really interested about your comments and depending of your feedbacks I will submit this question to Kathy and Bert.

Here's the question extracted from K&B Study Certify Guide for Java 5, chapter 10, page 796, question 10.

If three versions of MyClass.java exist on a file system:

Version 1 is in /foo/bar
Version 2 is in /foo/bar/baz
Version 3 is in /foo/bar/baz/bing

And the system's classpath includes:

/foo/bar/baz

And this command line is invoked from /foo :

javac -classpath /foo/bar/baz/bing:/foo/bar MyClass.java

Which version will be used by javac?

A. /foo/MyClass.java
B. /foo/bar/MyClass.java
C. /foo/bar/baz/MyClass.java
D. /foo/bar/baz/bing/MyClass.java
E. The result is not predictable.

The correct answer is D but I wonder why not A.

Here's my supposition : Once -classpath option used in javac is used only to provide search path to .class files, the .java sarch path (which is currently /foo) will not be affected. So javac first finds the .java file in the current directory /foo and then looks for java classes in the paths provided in class path.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
One problem though is that according to the statement of the problem, there is no MyClass.java in /foo. I created a test of this problem exactly as it's stated, and this is what I get from the compiler.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hey Edisandro,

I haven't looked at this question, so I can't confirm or deny if there is a problem with it, but I WILL say that Keith is on the right track by actually setting the situation up on his machine and testing it! In general we recommend that you write lots of little Java programs to test the topics you're studying, and with this topic it's especially useful! So, you might be right, but you'll learn the most if you test it with your own compiler!

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
javac -classpath /foo/bar/baz/bing:/foo/bar MyClass.java
^
D is right because your path name starts with "/", javac starts from the root and searches first in /foo/bar/baz/bing , javac find the file MyClass.java ( who is in /foo/bar/baz/bing/*) and compiles it...Its like the normal way you do with Linux-Konsole.


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Does this work with javac? I have been unable to get the compiler to compile a source file that was in a different directory just using the CLASSPATH to identify the directory it is in.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Keith and Bert,

Before ask everybody here about this question I also tested on my computer, as I always do, and i realized that the question no longer mention any version of .java file in /foo directory.

That's why I submitted my doubt.

Certainly I should mention that I tested on my computer, as Keith did, but I just didn't it because the focus of my question was on .java version that the original question considers as correct instead of .java version missing in /foo directory, which for me is implicit.

Anyway, thanks Keith and Bert for make me realize that even implicit issues sometimes must be told. I will remember that.
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Keith, you're right, according to the question there's no way to compile the MyClass.java in any of the directories when we are in foo, because when using classpath we're specifing path to classes not source files and we'll get an error.

Difference between java and javac is when compiling a source file using javac we don't have to specify the current directory to search for source file, because it's searched by default. What we have to do is to include in classpath correct path to classes needed to compile this file. If we are exectuing javac from a different directory than a directory we have our source file in, then besides classpath to find classes, we have to give (after classpath) the correct path to our source file.

Though when executing java and using classpath, even if we are in the same directory as our source file, we have to specify the current directory in classpath in order to find the class file.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Ah shucks, I think you guys found an errata

I'm looking into what we had in mind when we wrote it, then we'll get back to you guys with the fix.

Sorry, but keep up the good work in finding these, and just notice what good learning opportunites they provide

Bert
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Bert, it's impossible to have no errata in 800 pages book, you did a great job with Kathy, and for luck you have "meticulous-with-attention-to-details-readers"

I can only guess, but I think what you had in mind was that MyClass.java should be MyClass.class and in foo directory should have been some source file SomeFile.java that needs MyClass.class to compile.

Then with given classpath:

javac -classpath /foo/bar/baz/bing:/foo/bar SomeFile.java

The question would be, which MyClass.class version will be used to compile SomeFile.java file?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hey Aleksander,

Thanks for your kind words

I think that you're correct as far as the intention of the question, but I'd hate to blow it twice

My guess is that when I take a closer look at it you will have exactly the right idea!

Thanks!

Bert
 
Consider Paul's rocket mass heater.
 
subject: Possible question eligible for errata.