This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 6 - Chapter 10 : Development 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 "SCJP 6 - Chapter 10 : Development" Watch "SCJP 6 - Chapter 10 : Development" New topic
Author

SCJP 6 - Chapter 10 : Development

Filipe Abalada
Greenhorn

Joined: Nov 08, 2004
Posts: 9
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 ?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
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"


regards,
Arka
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
java -classpath MyJar.jar MyFile.java

Why would you use java to talk to compiler???


Programmer Analyst || J2EE web development/design
Filipe Abalada
Greenhorn

Joined: Nov 08, 2004
Posts: 9
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).
 
Don't get me started about those stupid light bulbs.
 
subject: SCJP 6 - Chapter 10 : Development
 
Similar Threads
use of javac and java commands
javac command (SCJP study guide book)
K&B doubt regarding question in Development chapter
Doubt at K&b chapter 10,question 11
Questions about classpath on SCJP book.