The following question is from the K&B Master Exam CD (Question 68)
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 the following: /foo/bar/baz
Which command line(s) will use Version 2 of MyClass.java? (Choose all that apply)
A javac -classpath /foo/bar:/foo/bar:. MyClass.java B javac -classpath /foo/bar/baz:/foo/bar MyClass.java C javac -classpath /foo/bar/baz/bing:/foo/bar:. MyClass.java D javac -classpath /foo/bar/baz/bing:/foo/bar/baz MyClass.java E The result is not predictable
B is given as the correct answer.
If I am correct the classpath is only used to search for .class files. If that is the case none the above answers are correct. Please let me know whether I am right or wrong.
the compiler also needs to know classpaths. How else could it find the standard libraries? If you have a program that imports other classes you wrote that are in packages, how else would the compiler find them? As well as the java launcher, the compiler also needs classpaths.
For the above question, you need to omit the system classpath, as this is overridden by a command line -classpath switch. And finally, remember that /foo/bar/baz is an absolute path, not a relative path, so the compiler looks in the baz directory, which resides at /foo/bar/baz. Best regards.
[ July 27, 2008: Message edited by: Keith Nagle ] [ July 27, 2008: Message edited by: Keith Nagle ]
so the compiler looks in the baz directory, which resides at /foo/bar/baz.
Yes, but what is the compiler looking for ? In my understanding it is looking for class files as the option -classpath indicates and not for source files. For the source file MyClass.java it will just look in the current directory, if the command is
I tested this out, and javac does not search the classpath for .java files. Only the current directory. You have to specify where MyClass.java is, unless it's in the current directory. So isn't the question incorrect? [ July 27, 2008: Message edited by: Eric Daly ]
Studying for SCJP 6
Joined: May 06, 2008
The question must be wrong. Assume that we are working with class files instead (MyClass.class) of .java source files and answer b is correct, also assuming we use the java launcher instead of javac. Classpaths on the command line override the system variable classpath and it will also evaluate the classpath from left to right. Regards [ July 27, 2008: Message edited by: Keith Nagle ]
Joined: May 11, 2008
Thanks. My assumption was the same that the question is wrong. Hope they will fix this in K&B.