• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

SCJP 6 - Chapter 10 : Development

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic