| Author |
K&B doubt regarding question in Development chapter
|
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
Hi guys,
I'm preparing for taking the SCJP 6, and during my study I found a question in K&B book whose answer i don't understand.
It is at page 817 , Question nr 11.
Given the following directory structure:
And given the contents of GetJar.java and Foo.java:
If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
Now, in the book A) is checked as a correct answer, but I don't find the logic.
I agree that javac will run withot errors but the second one "javac GetJar" I think will give an error as the classpath doesn't contain " . " and java will not find a GetJar class file .
Any suggestion?
thanks a lot.
|
SCJP 6.0 (88%)
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
This is an error in the book, it has been discussed many times like here.
[Edit: misread the question]
second one "javac GetJar" I think will give an error as the classpath doesn't contain " . "
I think you want to say "java GetJar". There's no problem with this as if you don't set the classpath, then javac and java commands automatically look into the current directory...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
Dragos Nica wrote:
Now, in the book A) is checked as a correct answer, but I don't find the logic.
I agree that javac will run withot errors but the second one "javac GetJar" I think will give an error as the classpath doesn't contain " . " and java will not find a GetJar class file .
Any suggestion?
first thing : you are compiling the java file right?? in other words you are generating a class file for the corresponding java file.
here you are compiling a java file which is inside a jar file .
just answer two questions :
so what will happen when the java file will be compiled ?
where will the generated class file go?? check it out and you will get to know why the second command is correct.
avi sinha
|
SCJP 5.0 SCWCD 5.0
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
Ankit Garg wrote:This is an error in the book, it has been discussed many times like here.
[Edit: misread the question]
second one "javac GetJar" I think will give an error as the classpath doesn't contain " . "
I think you want to say " java GetJar". There's no problem with this as if you don't set the classpath, then javac and java commands automatically look into the current directory...
Yes, I wanted to say "java GetJar" in my previous post. Sorry about that.
But I still don't understand. I tried to compile and run this code and it compiles fine, but it failed to run using:
Also I know that when searching for class files, the java and javac commands don't search the current directory by default, so we have to specify a classpath..
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
after compiling just check whether a class file is there in the current directory or not??
avi sinha
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
first thing : you are compiling the java file right?? in other words you are generating a class file for the corresponding java file.
here you are compiling a java file which is inside a jar file .
just answer two questions :
so what will happen when the java file will be compiled ?
where will the generated class file go?? check it out and you will get to know why the second command is correct.
avi sinha
yes, I'm compiling the java file.Please note that the java file is not inside the jar file, it is ouside . The generated class file is also outside the jar file.
In my test directory I have :
Foo.class
GetJar.class
GetJar.java
myApp - this is a subfolder and jar file contain actually this subfolder
MyJar.jar
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
avi sinha wrote:after compiling just check whether a class file is there in the current directory or not??
avi sinha
Yes, it is.
bellow is an output from my terminal:
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
Ok ... I got it.
The class file Foo.class was missing in my tar file. I rebuild the tar file and is working now.
Thank you all.
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
Dragos Nica wrote:
Ok ... I got it.
The class file Foo.class was missing in my tar file. I rebuild the tar file and is working now.
Thank you all.
I hurry when I thought I understood ..
It actually worked because I had set the current directory in classpath in my os environment
echo $CLASSPATH
.:/home/dragos/SCJP
In my point of view this set of commands will only work if "Foo.class" is located in myApp subdirectory and not in MyJar.jar
If "Foo.class" is located only in jar file , then i should specify" java -classpath MyJar.jar GetJar "
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
In my point of view this set of commands will only work if "Foo.class" is located in myApp subdirectory and not in MyJar.jar
If "Foo.class" is located only in jar file , then i should specify" java -classpath MyJar.jar GetJar "
That's right. The Compiler/JVM will only look inside a jar file only if you specify it...
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
So, I'm assuming that the answer is wrong, as in the question is only specified that myApp/Foo.class is placed in a JAR, but is doesn't say anything about Foo.class is placed also in myApp subdirectory.
Or should I understand this by default?
Dragos
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
The question doesn't says that there's Foo.class outside of the JAR file, that's why it is required to include the jar file in the classpath for the program to run properly. As I said earlier, this is an error in the book...
|
 |
Dragos Nica
Ranch Hand
Joined: Oct 25, 2009
Posts: 39
|
|
Ankit Garg wrote:The question doesn't says that there's Foo.class outside of the JAR file, that's why it is required to include the jar file in the classpath for the program to run properly. As I said earlier, this is an error in the book...
Ok! Topic Closed
Thanks all for replies
|
 |
 |
|
|
subject: K&B doubt regarding question in Development chapter
|
|
|