File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ash Gill
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book says that option A is the correct answer. Are you saying that the question is wrong?
 
Gaurav Sagar
Ranch Hand
Posts: 97
Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
Sagar Shroff
Ranch Hand
Posts: 209
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 1
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database
  • 2
  • Mark post as helpful
  • send pies
  • 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
 
fadi aboona
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Norbert Muench
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic