• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B doubt regarding question in Development chapter

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Dragos Nica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after compiling just check whether a class file is there in the current directory or not??

avi sinha
 
Dragos Nica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic