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

/foo/bar/baz

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.

Why is there a colon in the middle of theclasspath - is that legal?

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

Yes, I think it should be a semicolon as well

Oh Ok . May be that question is based on Linux machines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.