• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B Master Exam Question 68

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

Perhaps the given choices should better be

A) java -classpath /foo/bar:/foo/bar:. MyClass
B) ...

Then the answer in K&B would be correct.
[ July 27, 2008: Message edited by: Ralph Jaus ]
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tyronne Fernando
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. My assumption was the same that the question is wrong. Hope they will fix this in K&B.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic