• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Class path question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I have two questions regarding excerise nr 10 in Chapter 10 in the Java 5 certification book. It says:

If three versions of MyClass.java exist on the 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.java

Which version will be used by java?

(...a few alternatives are listed...)

The right answer is said to be:
/foo/bar/baz/bing/MyClass.java

My first question is:
Is it not supposed to be a javac command (not java) since we are compiling a java-file?

My second question is:
I thought classpaths were only used to locate .class files, not .java-files? Have I understood wrong?

Many thanks
Hanna
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hanna,

Both javac and java read the same classpath.

On the fly while launching the program you tell the classpath like you hava done; it over-writes the pre setting


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



An the vital part of the answer is, the sequence javac or java follows,
as you told there are three versions of MyClass class in the following:


Version 1 is in /foo/bar
Version 2 is in /foo/bar/baz
Version 3 is in /foo/bar/baz/bing



In your classpath setting what comes first, that is "/foo/bar/baz/bing" so therefore the class file living there will be used.

Your pre setting of classpath works only when you dont use -classpath on the fly while launching your application using -classpath or -cp.


Answer to your second question:
You know, classes living in the java.lang package are automatically accessible to your application and you need not to import them. But most of the time you are referring to class staying in some other standard packages
as java.util or even your own build packages so, you need to tell the compiler that at particular location, class files can be found. So the classpath does.



With Regards,
cmbhatt
[ April 07, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the ".java" should be removed. The problem was noticed here.

It's a case of an errata to a fix to an errata.

I would say it should be:

If three versions of MyClass.class exist on the 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?

(...a few alternatives are listed...)

The right answer is said to be:
/foo/bar/baz/bing/MyClass.class


[ April 07, 2007: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And classpaths are used for finding class files, jar files, and other resources used by the compiler, jvm, and other tools. There is also -sourcepath for finding java source files.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,

I have "K&B" Book for SCJP exam 310-055.
dreamtech PRESS, India, 2006 Edition.

1- What goes wrong if the question still remains "Which version will be used by javac?"

2- Is this question asking about, which class file will be used by the java?

Command line invocation is given as
javac -class /foo/bar/baz/bing:/foo/bar MyClass.java

3- There will be only MyClass.class file and it will be in the foo/bar/baz/bing, as per the given
javac invocation.Right? Or we should treat like .class files are there in each package.

I have already followed the errata and corrected pages of my book.

4- What is the actual question, Is its concern on javac or java?
5- Wont javac search for .java as per the -classpath says to be?

Please make me clear on this issue!!!


Thanks and Regards,
cmbhatt
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is meant to be about how the java command finds the class files.

The original book had the javac command. As noted in the Errata it should be the java command. It seems like Hanna and I have a more recent printing (in my case the 6th) of the book where the javac command has been corrected to be the java command. However, there is still a problem with the question - it uses ".java" instead of ".class" files.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry,

I got that!

With Regards,
cmbhatt
 
Hanna Barenthin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Now it is much more clear!

BR,
Hanna
 
"To do good, you actually have to do something." -- Yvon Chouinard
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic