Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't run?

 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

test.xml:
<target name="run" depends="">
<java jar="Test.jar" fork="true"/>
</target>

why? i am trying to solve it two days, please for help.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ant cannot find log4j's library. You have to set the classpath dependencies in your build.xml. Check this tutorial (Using external libraries).
 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

i still confused.thank you.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your manifest file ? What does it contain ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch I see this thread has been (correctly) moved to the "ANT" forum.
 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch I see this thread has been (correctly) moved to the "ANT" forum.

yes,you are right, it's a mistake. i am fresher.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have to set the dependency in your MANIFEST file. Try to use this instead :

 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:You still have to set the dependency in your MANIFEST file. Try to use this instead :


Hi,if follow this, it is still wrong. whatever i modify.

why?
Test.jar contains below:
Test.class Log4j.properties log4j-1.2.14.jar META-INF
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
Class-Path: log4j-1.2.14.jar
what's wrong? thanks a lot.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try "java classname="${Main-class}" fork="true"" instead of "java jar="${build.lib}/${Main-class}.jar" fork="true"".

It would be so much simplier if you kept the log4j JAR outside of your own JAR Do you need to put it inside ?
 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Try "java classname="${Main-class}" fork="true"" instead of "java jar="${build.lib}/${Main-class}.jar" fork="true"".

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

Test.jar contains: Test.class Log4j.properties META-INF

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?
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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~
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yancy,

Just so you know it's really not recommended to put 3rd party JARs in the JRE folders, they should be kept elsewhere and referenced as needed .
 
Yancy cheng
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martijn Verburg wrote:Hi Yancy,

Just so you know it's really not recommended to put 3rd party JARs in the JRE folders, they should be kept elsewhere and referenced as needed .


yes,you are right,very thanks~
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic