File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP 6 - Chapter 10 : Development

 
Filipe Marques
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Shashank Rudra
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -classpath MyJar.jar MyFile.java

Why would you use java to talk to compiler???
 
Filipe Marques
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic