aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions about classpath on SCJP book. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions about classpath on SCJP book." Watch "Questions about classpath on SCJP book." New topic
Author

Questions about classpath on SCJP book.

Jack Robertson
Greenhorn

Joined: Aug 03, 2011
Posts: 27
If you have the scjp book for java 6 exam authored by kathy and berts, it will be much easier for you to answer.

On page 830, the answer for question 12 is F and G, cause the command line must find both GoDeep and Foo. So I think that means the reason C is incorrect is that it lacks "." to search for the current directory to find GoDeep.

But on page 804, in the middle part where it shows us how to find the jar file while compiling, it says that
"cd test
javac -classpath ws/myApp.jar UseStuff.java"
and this time why there's no "." in the classpath? I thought you were supposed to find both the jar file and .java file that is in the current directory.

And back on page 829 for question 11, the answer says "C is wrong is because the -classpath MyJar.java in the java invocation does not include the test directory. First of all, there's no MyJar.java file in the question. Secondly,if there's a typo and it actually means MyJar.jar,then why can the javac invocation skip the current directory which is test while the java invocation has to include the directory test?

I'm really confused by some questions of this chapter cause I really can't find enough materials in the same section of the book.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

Welcome to the Ranch, Jack Robertson!

I don't have the book with me now, but I'll try to answer some here.
...So I think that means the reason C is incorrect is that it lacks "." to search for the current directory to find GoDeep,right?

If "GoDeep" class file is in the current directory where java is executed then yes.
But on page 804, in the middle part where it shows us how to find the jar file while compiling, it says that
"cd test
javac -classpath ws/myApp.jar UseStuff.java"
and this time why there's no "." in the classpath? I thought you were supposed to find both the jar file and .java file that is in the current directory.

"-classpath" switch is to find the class files/jar files etc... not for the source files.
Secondly,if there's a typo and it actually means MyJar.jar,then why can the javac invocation skip the current directory which is test while the java invocation has to include the directory test?

Hard to comment without looking at how the question is formed here.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jack Robertson
Greenhorn

Joined: Aug 03, 2011
Posts: 27
Thanks for the reply. I just typed the questions in the book this time,
Page 830, "Question12"
Given the following directory structure:
x-|
---|-GoDeep.class
---|-test-|
-----------|-MyJar.jar
-----------|
-----------|-myApp-|
----------------------|-Foo.jar
----------------------|-Foo.class

And given the contents of GoDeep.java and Foo.java:
3 public class GoDeep{
4 public static void main(String[] args){
5 System.out.println(myApp.Foo.d);
6 }
7 }

3 package myApp;
4 public class Foo {public static int d=8;}
And MyJar.jar contains the following entry: myApp/Foo.class
If the current directory is x, which commands will successfully execute GoDeep.class and produce the output 8?

A.java GoDeep
B.java -classpath . GoDeep
C.java -classpath test/MyJar.jar GoDeep
D.java GoDeep -classpath test/MyJar.jar
E.java GoDeep -classpath test/Myjar.jar:.
F.java -classpath .:test/MyJar.jar GoDeep
G.java -classpath test/MyJar.jar:. GoDeep

The answer is F and G, cause the command line must find both GoDeep.class and Foo.class. So i guess the reason C is wrong is that it lacks "." and can't find the current directory.


Page 829,"Question 11":
Give the following directory structure:
test-|
------|-GetJar.jar
------|
------|-MyApp-|
-----------------|-Foo.java

And given the contents of GetJar.java and Foo.java:
3 public class GetJar{
4 public static void main(String[] args){
5 System.out.println(myApp.Foo.d);
6 }
7 }

3 package myApp;
4 public class Foo {public static int d=8;}
If the current directory is test, and myApp/Foo.class is placed in a JAR file called MyJar.jar located in test, which sets of command will compile GetJar.java and produce the output 8?

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 A. B and D are incorrect cause javac MyJar.jar GetJar.java is wrong syntax.C is wrong because the -classpath MyJar.jar in the java invocation does not include the test directory.

Here are my questions,
I.In question12 it says you need to use -classpath "." to find the class in the current directory and the class in the jar file while invoking the java command, but how come in question11 there's no "." in any of the -classpath command when you invoke the javac command?

II. In question11, the answer says in the java invocation C doesn't have the test directory, but look at B and D, none of the javac invocation in these two include the test directory, and why?


Thanks for the reply.
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
Question I


Page 798 of K&B Text

This classpath is identical to the previous one EXCEPT that the dot (.) at the end
of the declaration instructs java or javac to also search for class files in the current
directory. (Remember, we're talking about class files—when you're telling javac
which .java file to compile, javac looks in the current directory by default.)



Same answer for Question II. B and D are already searching test because test is the current directory.
Jack Robertson
Greenhorn

Joined: Aug 03, 2011
Posts: 27
Thanks,man.
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
No prob. It was fresh in my head from reading it a couple days ago.
Alexander Weickmann
Greenhorn

Joined: Sep 01, 2013
Posts: 6
Regarding Question 11, I tested this out and it still seems a bit weird to me.

Yes, answer A actually works:
javac -classpath MyJar.jar GetJar.java
java GetJar

However, this seems to work only because the javac call actually compiles the file myApp/Foo.java as well. So in the call java GetJar, the class file myApp/Foo.class is used, instead of the same file within the MyJar.jar. At least, java GetJar does not work any longer as soon as I delete myApp/Foo.class. Can anyone please confirm this? Also, why is the javac command "recursively" compiling myApp/Foo.java??
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
javac is not compiling myApp/Foo.java in this case of "javac -cp MyJar.jar GetJar.java"

-classpath xxx.jar means the javac will go to xxx.jar to find myApp.Foo in order to compile GetJar.java

java GetJar works in this case because your current directory is test. The java will go to the current directory to find GetJar.class.

One more note: Suppse HelloWorld.class is in somedirectory, java -cp somedirectory; HelloWorld means the java will go to somedirectory to find HelloWorld.class to execute.
 
Consider Paul's rocket mass heater.
 
subject: Questions about classpath on SCJP book.