noel yim

Greenhorn
+ Follow
since Sep 04, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by noel yim

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
Yes. If you read my post here
java -cp .;MyJar.jar GetJar <--- won't work
java -cp MyJar.jar;. GetJar <--- works
why?

My post has one further question about Q12, both works. Why?

Thank you very much.
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?
I also have doubts about this question
And a further question about the .;MyJar.jar vs MyJar.jar;. unanswered.
Any suggestions?

[ September 05, 2008: Message edited by: noel yim ]
[ September 05, 2008: Message edited by: noel yim ]
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 ]
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)
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?
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 ]