• 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

chapter 10 question number 11 k&b book (on jar files)

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

the answer is option A, i have tried and it works perfectly fine if the myapp has got a class Foo.class. can anyone please confirm if there has been some mistake found in the question.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book says that option A is the correct answer. Are you saying that the question is wrong?
 
Ranch Hand
Posts: 97
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the answers are wrong. It should be:-

javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar;. GetJar


Regards,
Gaurav
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ogeh ikem wrote:The book says that option A is the correct answer. Are you saying that the question is wrong?



yes, i have tried it out. for option A to be correct, the "myapp" in the given directory structure should have a "Foo.class". OR as gaurav sagar said there should be an option

javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar;. GetJar



thanks gaurav & ogeh

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gaurav Sagar wrote:All the answers are wrong. It should be:-

javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar;. GetJar


Regards,
Gaurav




Why is the specification of current directory a necessary???
 
Ash Gill
Ranch Hand
Posts: 71
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Shroff wrote:
Why is the specification of current directory a necessary???



Hi sagar, after compiling GetJar.java using:
javac -classpath MyJar.jar GetJar.java
we get a GetJar.class file in the current directory. So, we need to specify the current directory for java to look for GetJar.class.

I would like to add that from my experience of trying numerous examples, ".class" files are searched in the current directory by default if we are not using the "-classpath" option. however, if we use the "classpath" option then the current directory is not searched by default for .class files.
 
Gaurav Sagar
Ranch Hand
Posts: 97
MySQL Database Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Shroff wrote:
.....

Why is the specification of current directory a necessary???



When you specify a classpath, it overrides the current classpath (this even includes the one you might have set using an environmental variable as in Windows), so you explicitly need to specify the classpath of the class that you want to run, which in the question above was the current directory.

Hope you got the point, Sagar.

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

Gaurav Sagar wrote:All the answers are wrong. It should be:-

javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar;. GetJar


Regards,
Gaurav



What i don't understand is how come we didn't use the "." in the javac command but java command wouldn't work without it?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ash Gill wrote:I would like to add that from my experience of trying numerous examples, ".class" files are searched in the current directory by default if we are not using the "-classpath" option. however, if we use the "classpath" option then the current directory is not searched by default for .class files.


That is correct. See How classes are found.
This can be easily verified by running the following code. If no class path is specified on the command line and none is set in the CLASSPATH env variable, then this will output "Class Path: ."

fadi aboona wrote:What i don't understand is how come we didn't use the "." in the javac command but java command wouldn't work without it?


Simple: The javac command wasn't looking for a GetJar.class file, it was creating it. The java command on the other hand needs to find the GetJar.class and it will do so by searching the class path.
 
fadi aboona
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norbert Muench wrote:

Ash Gill wrote:I would like to add that from my experience of trying numerous examples, ".class" files are searched in the current directory by default if we are not using the "-classpath" option. however, if we use the "classpath" option then the current directory is not searched by default for .class files.


That is correct. See How classes are found.
This can be easily verified by running the following code. If no class path is specified on the command line and none is set in the CLASSPATH env variable, then this will output "Class Path: ."

fadi aboona wrote:What i don't understand is how come we didn't use the "." in the javac command but java command wouldn't work without it?


Simple: The javac command wasn't looking for a GetJar.class file, it was creating it. The java command on the other hand needs to find the GetJar.class and it will do so by searching the class path.



Thanks Norbert, i guess i have to read more about classpath.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic