Daniel Negut wrote:You probably have the CLASSPATH environment variable defined (and the test folder is not included).
Question 11 assumes that there is no CLASSPATH environment variable defined.
Software Developer, Oracle Java Certification Training Labs at EPractize Labs.
gurpeet singh wrote:
1. Given the following directory structure:
test-|
|- GetJar.java
|
|- myApp-|
|-Foo.java
And given the contents of GetJar.java and Foo.java:
3. package myApp;
4. public class Foo { public static int d = 8; }
If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar
located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
Answer:
✓ A is correct. Given the current directory and where the necessary files are located, these
are the correct command line statements.
B and D are wrong because javac MyJar.jar GetJar.java is incorrect syntax. C is wrong
because the -classpath MyJar.java in the java invocation does not include the test directory.
the book says answer is A. but when i ran the program with proper structure it came out that none of the option is correct. the actual answer is with a little modification on option C as javac -classpath MyJar.jar GetJar.java, java -classpath MyJar.jar:. GetJar. here what i changed was added current directory to classpath. the answer A is not correct because GetJar needs myApp/Foo which is in MyJar.jar but we havent specified the classpath for the jar file MyJar.jar. please confirm the same ??
javac -classpath MyJar.jar GetJar.java
java GetJar
gurpeet singh wrote:guys i double checked it again. i have no classpath set. in the answer given as option A as follows :
javac -classpath MyJar.jar GetJar.java
java GetJar
the first part i.e. compilation is file. error comes when we run GetJar using java GetJar. since we are in test directory so when we do java GetJar , it will be default classpath(i do not have environment variable set). so it will look for GetJar in the current directory(test) which will be found. but GetJar needs Foo class which is in MyJar.jar , which too is in test (current) directory. but it is not able to find that Foo class . so i think we have to specify jar file on the classpath as well as the current directory. i ran program twice using the exact scenario given in the book on two different jvm's but it worked the same and gave me error when i ran GetJar. so the only option is if someone else run it or the answers given are wrong .
W Lin wrote:I just verified, yes you are correct.
I guess, in this question, when people make the MyJar.jar, they don't remove the compiled Foo.class from myApp folder after making MyJar.jar, thus it makes "java GetJar" work in answer (A)
gurpeet singh wrote:guys i double checked it again. i have no classpath set. in the answer given as option A as follows :
javac -classpath MyJar.jar GetJar.java
java GetJar
the first part i.e. compilation is file. error comes when we run GetJar using java GetJar. since we are in test directory so when we do java GetJar , it will be default classpath(i do not have environment variable set). so it will look for GetJar in the current directory(test) which will be found. but GetJar needs Foo class which is in MyJar.jar , which too is in test (current) directory. but it is not able to find that Foo class . so i think we have to specify jar file on the classpath as well as the current directory. i ran program twice using the exact scenario given in the book on two different jvm's but it worked the same and gave me error when i ran GetJar. so the only option is if someone else run it or the answers given are wrong .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |