File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes chapter 10 question number 11 k&b book (on jar files) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "chapter 10 question number 11 k&b book (on jar files)" Watch "chapter 10 question number 11 k&b book (on jar files)" New topic
Author

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

Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
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

Joined: May 13, 2002
Posts: 180
The book says that option A is the correct answer. Are you saying that the question is wrong?
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

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

Joined: Jun 30, 2011
Posts: 71
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

Joined: Jun 07, 2011
Posts: 202

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

Joined: Jun 30, 2011
Posts: 71
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

Joined: Sep 08, 2010
Posts: 97

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

Joined: Apr 25, 2010
Posts: 71
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

Joined: Mar 09, 2012
Posts: 19
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

Joined: Apr 25, 2010
Posts: 71
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
 
subject: chapter 10 question number 11 k&b book (on jar files)
 
Similar Threads
K&B doubt regarding question in Development chapter
Question related to self test question in SCJP (Sierra/Bates) Java 6 book
Doubt at K&b chapter 10,question 11
Mistake in GuideBook for SCJP Java 6.0, Chapter 10, Self Test question 11
Confusion over java classpath