Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scjp6 ch10 q.11

 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW,since Foo.java belongs to myApp package, I compiled Foo.java outside the myApp directory, like this:
javac myApp/Foo.java

Should I have compile Foo.java inside the myApp directory, like this:
javac Foo.java

(v)Could you explain the difference in detail?
 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the exception i saw in a. was:
>java GetJar
Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static field myApp.Foo.d
at GetJar.main(GetJar.java:3)
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Noel...I think there is a problem in this question..I have posted it but I am not getting any proper replies...


As far as option c goes, it is wrong. The class in which the main method is, must be in the classpath. by default java command searches for class files in the current directory. but if you use java -classpath MyJar.jar GetJar then it will search for GetJar.class only in MyJar.jar file and not in the current directory.

Secondly you compiled the file Foo.java just fine. You must compile .java files from the parent directory of the root of the package.

Thirdly if d was not static, you would need to access it using an instance of class Foo as you did...
 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, following your suggestion, I tried :
D:\Documents and Settings\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp
MyJar.jar;. GetJar
8

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp
.;MyJar.jar GetJar
Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static field myApp.Foo.d
at GetJar.main(GetJar.java:3)
(***)I see why i have this error, i had modified Foo.jar that d was no longer static. After I fixed it. it works


D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp
.;MyJarNonStatic.jar GetJarNonStatic
8

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp
MyJarNonStatic.jar;. GetJarNonStatic
8


So looks like if a static is needed, it has to be search(found in MyJar.jar) before the class with main (in this case GetJar).
I am just guessing, I can't find any reference for this.
Can anyone explain the reason for the above code? Figured it out, see (***)

[ September 08, 2008: Message edited by: noel yim ]
[ September 08, 2008: Message edited by: noel yim ]
 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange thing is when I look at question 12. It actually works both ways:
D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q12\x>javac -cp test\MyJar.jar GoDeep.java

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q12\x>java -cp test\MyJar.jar;. GoDeep
8

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q12\x>java -cp .;test\MyJar.jar GoDeep
8

So, how come question 11 does not run with '.;MyJar.jar' but question 12 works with '.;test\MyJar.jar' ?

Is it because GetJar.java was compiled at the same dir level as the jar file but DoDeep.jar was compiled at the higher dir level as jar file?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well actually I think the error is happening because of compilation version. Recompile both files, then add Foo.class into MyJar.jar and then run GetJar.class.
 
noel yim
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I did the follow and this time works:

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test\myApp>javac Foo.java
Or
D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>javac myApp\Foo.java

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test\myApp>cd ..

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>jar -cf MyJar.jar myApp

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>javac -cp MyJar.jar GetJar.java


D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp .;MyJar.jar GetJar
8

D:\Documents and Settings\nyim\My Documents\SCJP_OCP\code\ch10_q11\test>java -cp MyJar.jar;. GetJar
8
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic