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 !
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.
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.
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"