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
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.
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!
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
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
Joined: Feb 07, 2005
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.
Joined: Jan 19, 2006
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.
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.
Joined: Oct 14, 2002
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
Joined: Nov 11, 2005
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.