Hi Team,

Im preparing for OCJP and in one of the sample question is below

Three version of MyClass.class exist on a file system

  1. /foo/bar/
  2. /foo/bar/baz
  3. /foo/bar/baz/bing

And the system class path includes


And this is the command invoked from /foo

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

Which version will be used by java ?

The answer says : /foo/bar/baz/bing/MyClass.class

But how is this possible, the command should be invoked from above "foo" directory right ? if its invoked from foo directory then it wont find foo.bar.baz.bing.MyClass right ? Please throw some light on this answer.

3 Years
Discussion Span
Last Post by murali2489

Hmm, no, answer is correct. You can watch that as Folders in MyComputer,
You have C:\ then you have User\ then you have Smile\ then you have Movies\ so your path is C:\User\Smile\Movies\ and you see content on disk in that folder in Java is the same way, he goes throuh structure of your package and find last one, then he search for file you asked...


Hi Milil,
Thanks for the advise.

Hi James,

That is legal as per book, it says it will search for the class file in directories before and after colon


Read more about classpath. classpath Documentation

From the documentation:

...Multiple path entries are separated by semi-colons....

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

Classpath entries that are neither directories nor archives (.zip or .jar files) nor '' are ignored.

Also, I think that in your first post, the colon in -classpath should be a semi-colon

Edited by cgeier: upate

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.