Monday, August 9, 2010

Classpaths: MyClass.class

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 10, Question 5

If three versions of MyClass.class 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
java -classpath /foo/bar/baz/bing:/foo/bar MyClass

Which version will be used by java?

  1. /foo/MyClass.class

  2. /foo/bar/MyClass.class

  3. /foo/bar/baz/MyClass.class

  4. /foo/bar/baz/bing/MyClass.class
    Correct. A -classpath included with a java invocation overrides a system classpath. When java is using any classpath, it reads the classpath from left to right, and uses the first match it finds.


  5. The result is not predictable.


No comments:

Post a Comment