Forums Register Login

SCJP 6 - Chapter 10 : Development

+Pie Number of slices to send: Send
Hello,

I'm comparing questions 11 and 12 and I do not understand the answers although they seem similar except the test subdirectory :

question 11: why question c is not correct ? The java command : java -classpath MyJar.jar GetJar is incorrect whereas question a : java GetJar is ok.
When compiling using javac -classpath MyJar.jar GetJar.java, the jar file was found... So does not it work with the java command the same way ?
Why the jar file is not found with java command ?

question 12 : I do not understand why the current directory has to be in the classpath to execute the java command ?
Indeed, in question 11, the java command did not need the current directory (.) to be set to run GetJar.java.
In this question, the right command has to include the (.) in the classpath in order to execute the GoDeep class.

If we compare both questions, it seems the mechanism is different...
I can't figure out why !

Someone could help me ?
+Pie Number of slices to send: Send
for java command:
1) if you donot use -classpath option explicitly, then by default it finds GetJar.class file in current directory.
2) if you use -classpath
java -classpath MyJar.jar GetJar
they you are saying java command to find GetJar.class in MyJar.jar file, so you need to set current directory explicitly here.
like:
java -classpath MyJar.jar;. GetJar

for javac command:
1) javac -classpath MyJar.jar GetJar.java
classpath is set for .class file not .java file, here javac command has to GetJar.java file and javac gets it in current directory.
Remember -classpath is used for .class files not for .java files.

+Pie Number of slices to send: Send
Hi,
The javac command is used on .java files and not .class files.We use javac to compile .java files to .class files BUT you cannot use -classpath option with .java files.
Consider the following example:

Here you are telling
"Mr.Compiler please compile MyFile.java if you need any reference(.class files), please find it in the current directory inside MyJar.jar"
+Pie Number of slices to send: Send
 

java -classpath MyJar.jar MyFile.java


Why would you use java to talk to compiler???
+Pie Number of slices to send: Send
Thank you a lot for your help.

Punit, your answer is very clear, thank you.

Arka, indeed Giffy is right, it should be :



(which makes reference to the .class file).
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1663 times.
Similar Threads
use of javac and java commands
Doubt at K&b chapter 10,question 11
K&B doubt regarding question in Development chapter
javac command (SCJP study guide book)
Questions about classpath on SCJP book.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:00:09.