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 must -classpath option help java find both main file and files it uses? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "must -classpath option help java find both main file and files it uses?" Watch "must -classpath option help java find both main file and files it uses?" New topic
Author

must -classpath option help java find both main file and files it uses?

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
This is a question from a study book (not the actual exam).

Directory structure:

and GetJar.java and Foo.java are




The current directory is "test" and myApp/Foo.class is placed in MyJar.jar located in "test", what will compile GetJar.java and produce output of 8?

According to this book, the right answer is this:
javac -classpath MyJar.jar GetJar.java
java GetJar
This seems strange to me because, GetJar needs to use Foo, and the java invocation does not provide a classpath to find the Foo class. Also, the javac invocation does not provide the classpath to get to GetJar.java.

Also, according to the book, this is a wrong answer:
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
And this seems strange to me, because according to the book, the classpath in the java invocation does not include the test directory. Apparently the javac line compiles successfully. But the javac and java invocations have exactly the same classpaths, so why does the javac command compile and the java command not run the program?
Arunkumar Chinnadurai
Ranch Hand

Joined: Dec 15, 2011
Posts: 56

You should give valid class path for that package..
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Kevin Tysen wrote:According to this book, the right answer is this:
javac -classpath MyJar.jar GetJar.java
java GetJar
This seems strange to me because, GetJar needs to use Foo, and the java invocation does not provide a classpath to find the Foo class. Also, the javac invocation does not provide the classpath to get to GetJar.java.

You are right. The java command should be specified a class path where it should find the foo class. Another assumption is that the folder structure myApp\Foo.class is present in the directory test which is referred by the class path for the java command. But you have explicitly said that classpath must be set & current execution directory is not taken as the classpath. Hence it should fail.

The javac invocation provides the classpath to get to Foo.java and not GetJar.java. The file GetJar.java is searched by the javac tool in the current working directory.

Also, according to the book, this is a wrong answer:
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
And this seems strange to me, because according to the book, the classpath in the java invocation does not include the test directory. Apparently the javac line compiles successfully. But the javac and java invocations have exactly the same classpaths, so why does the javac command compile and the java command not run the program?


The javac command is able to find Foo.class file from the given classpath MyJar.jar & hence it succeeds.

The java command tries to find the GetJar[.class] file in the specified classpath - MyJar.jar. But since the GetJar.class is present in the test directory and not in MyJar.jar file, it fails.

Differentiate between the below two -



My advise is that you should write code, compile and try the scenarios to understand it better.
 
wood burning stoves
 
subject: must -classpath option help java find both main file and files it uses?
 
Similar Threads
Mistake in GuideBook for SCJP Java 6.0, Chapter 10, Self Test question 11
K&B 1.6 Chapter 10 Q 11
Chapter 10 Question 11 of SCJP K and B
K&B doubt regarding question in Development chapter
Doubt at K&b chapter 10,question 11