• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Possible question eligible for errata.

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

I'd like to discuss with you the possibility to submit the below question to Kathy and Bert as a candidate for an errata.

Recently I opened a thread reporting this but I received only one concrete opinion about the question and such opinion also confirmed my supposition that the question would be eligible for an errata.

So, I'm really interested about your comments and depending of your feedbacks I will submit this question to Kathy and Bert.

Here's the question extracted from K&B Study Certify Guide for Java 5, chapter 10, page 796, question 10.

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:

/foo/bar/baz

And this command line is invoked from /foo :

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

Which version will be used by javac?

A. /foo/MyClass.java
B. /foo/bar/MyClass.java
C. /foo/bar/baz/MyClass.java
D. /foo/bar/baz/bing/MyClass.java
E. The result is not predictable.

The correct answer is D but I wonder why not A.

Here's my supposition : Once -classpath option used in javac is used only to provide search path to .class files, the .java sarch path (which is currently /foo) will not be affected. So javac first finds the .java file in the current directory /foo and then looks for java classes in the paths provided in class path.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem though is that according to the statement of the problem, there is no MyClass.java in /foo. I created a test of this problem exactly as it's stated, and this is what I get from the compiler.

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Edisandro,

I haven't looked at this question, so I can't confirm or deny if there is a problem with it, but I WILL say that Keith is on the right track by actually setting the situation up on his machine and testing it! In general we recommend that you write lots of little Java programs to test the topics you're studying, and with this topic it's especially useful! So, you might be right, but you'll learn the most if you test it with your own compiler!

hth,

Bert
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac -classpath /foo/bar/baz/bing:/foo/bar MyClass.java
^
D is right because your path name starts with "/", javac starts from the root and searches first in /foo/bar/baz/bing , javac find the file MyClass.java ( who is in /foo/bar/baz/bing/*) and compiles it...Its like the normal way you do with Linux-Konsole.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this work with javac? I have been unable to get the compiler to compile a source file that was in a different directory just using the CLASSPATH to identify the directory it is in.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith and Bert,

Before ask everybody here about this question I also tested on my computer, as I always do, and i realized that the question no longer mention any version of .java file in /foo directory.

That's why I submitted my doubt.

Certainly I should mention that I tested on my computer, as Keith did, but I just didn't it because the focus of my question was on .java version that the original question considers as correct instead of .java version missing in /foo directory, which for me is implicit.

Anyway, thanks Keith and Bert for make me realize that even implicit issues sometimes must be told. I will remember that.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith, you're right, according to the question there's no way to compile the MyClass.java in any of the directories when we are in foo, because when using classpath we're specifing path to classes not source files and we'll get an error.

Difference between java and javac is when compiling a source file using javac we don't have to specify the current directory to search for source file, because it's searched by default. What we have to do is to include in classpath correct path to classes needed to compile this file. If we are exectuing javac from a different directory than a directory we have our source file in, then besides classpath to find classes, we have to give (after classpath) the correct path to our source file.

Though when executing java and using classpath, even if we are in the same directory as our source file, we have to specify the current directory in classpath in order to find the class file.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah shucks, I think you guys found an errata

I'm looking into what we had in mind when we wrote it, then we'll get back to you guys with the fix.

Sorry, but keep up the good work in finding these, and just notice what good learning opportunites they provide

Bert
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert, it's impossible to have no errata in 800 pages book, you did a great job with Kathy, and for luck you have "meticulous-with-attention-to-details-readers"

I can only guess, but I think what you had in mind was that MyClass.java should be MyClass.class and in foo directory should have been some source file SomeFile.java that needs MyClass.class to compile.

Then with given classpath:

javac -classpath /foo/bar/baz/bing:/foo/bar SomeFile.java

The question would be, which MyClass.class version will be used to compile SomeFile.java file?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Aleksander,

Thanks for your kind words

I think that you're correct as far as the intention of the question, but I'd hate to blow it twice

My guess is that when I take a closer look at it you will have exactly the right idea!

Thanks!

Bert
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic