Hi,guys. i meet an error:
Successful:
C:\test>java -jar Test.jar
INFO (Test) - haha
Failure:
C:\test>ant -buildfile test.xml
run:
[java] java.lang.NoClassDefFoundError: org/apache/log4j/Logger
[java] at Test.<clinit>(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[java] ... 1 more
[java] Could not find the main class: Test. Program will exit.
[java] Exception in thread "main"
[java] Java Result: 1
Christophe Verré wrote:Ant cannot find log4j's library. You have to set the classpath dependencies in your build.xml. Check this tutorial (Using external libraries).
Hi,thanks for your reply. i know what you mean. but you can see the first runs ok, but the second runs meeting an error. why?
my Test.jar contains below:
Test.class
log4j-1.2.14.jar
log4j.properties
META-INF
Where is your manifest file ? What does it contain ?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
Welcome to the Ranch I see this thread has been (correctly) moved to the "ANT" forum.
Yancy cheng
Greenhorn
Joined: Aug 18, 2010
Posts: 10
posted
0
Christophe Verré wrote:Where is your manifest file ? What does it contain ?
Thanks very much for your kindly reply. my project works all right now, but i still have some questions to ask you.
build.xml
please see run target: when i used <java jar="${build.lib}/${Main-class}.jar" fork="true"/> i will meet the "NoClassDefFoundError", why,even i have modified the
Yancy cheng
Greenhorn
Joined: Aug 18, 2010
Posts: 10
posted
0
Campbell Ritchie wrote:Welcome to the Ranch I see this thread has been (correctly) moved to the "ANT" forum.
It would be so much simplier if you kept the log4j JAR outside of your own JAR Do you need to put it inside ?
you are very kind! Thanks very much. but i still want to know why? Because when i go deep into the question, i find a more confused problem:
i move the Test.jar to the C:\test
MANIFEST.MF contains below:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 17.0-b17 (Sun Microsystems Inc.)
Built-by: Yancy
Main-Class: Test
(no dependency on log4j.jar)
Successful: C:\test>java -jar Test.jar
INFO (Test) - haha
Failure: C:\Java\jdk1.6.0_21\bin>java.exe -jar C:\test\Test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at Test.<clinit>(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
Could not find the main class: Test. Program will exit.
it makes me so surprised! i just want to know why?
This message was edited 1 time. Last update was at by Yancy cheng
Because your log4j jar is located in C:\test
Execute C:\Java\jdk1.6.0_21\bin>java.exe -jar C:\test\Test.jar
would not include the log4j libaray
Execute C:\test>java -jar Test.jar
would do because the log4j library is on the current path "C:\test"
Yancy cheng
Greenhorn
Joined: Aug 18, 2010
Posts: 10
posted
0
Raymond Tong wrote:Because your log4j jar is located in C:\test
Execute C:\Java\jdk1.6.0_21\bin>java.exe -jar C:\test\Test.jar
would not include the log4j libaray
Execute C:\test>java -jar Test.jar
would do because the log4j library is on the current path "C:\test"
thanks for you reply, but log4j.jar is not located in C:\test,it is not as easily as you think. i put log4j.jar to the jre/lib/exc. So the two ways of execute should be the same, but the results are quite different, i wanna to see why? thanks all the same~