Foo.java:
package myApp;
public class Foo{public static int d =8;}
GetJar.java
public class GetJar{
public static void main(
String[] args){
System.out.println(myApp.Foo.d);
}
}
test-|
-----|-GetJar.java
-----|-MyApp-|
-------------|-Foo.java
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 comments will compile GetJar.java and produce the output 8?
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
I thought the answer is c.(which classpath is needed for 'java') however, answer is a.
The reason c. is wrong: because the -classpath MyJar.jar in the 'java' invocation does not include the 'test' directory.
(i) Can anyone explain further?
(ii)I tired to run the code, c. give me the following:
Exception in
thread "main" java.lang.NoClassDefFoundError: GetJar
Caused by: java.lang.ClassNotFoundException: GetJar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
(iii)Can anyone explain the difference if int d is not static?
eg.
GetJarNonStatic.java:
import myApp.Foo;
public class GetJarNonStatic{
public static void main(String[] args){
System.out.println(new Foo().d);
}
}
Foo.java:
package myApp;
public class Foo{public int d =8;}
MyJar.jar has myApp/Foo.class
(iv)Then, what should be the commands for successfully compile and produce output 8?
Thanks a lot.
[ September 04, 2008: Message edited by: noel yim ]
[ September 04, 2008: Message edited by: noel yim ]