This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes Why can't run? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Why can Watch "Why can New topic
Author

Why can't run?

Yancy cheng
Greenhorn

Joined: Aug 18, 2010
Posts: 10
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Ant cannot find log4j's library. You have to set the classpath dependencies in your build.xml. Check this tutorial (Using external libraries).


[My Blog]
All roads lead to JavaRanch
Yancy cheng
Greenhorn

Joined: Aug 18, 2010
Posts: 10
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Where is your manifest file ? What does it contain ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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
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
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

You still have to set the dependency in your MANIFEST file. Try to use this instead :

Yancy cheng
Greenhorn

Joined: Aug 18, 2010
Posts: 10
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Aug 18, 2010
Posts: 10
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

Joined: Aug 15, 2010
Posts: 230
    
    2

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
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

Joined: Jun 24, 2003
Posts: 3274
    
    5

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 .


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Yancy cheng
Greenhorn

Joined: Aug 18, 2010
Posts: 10
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~
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't run?
 
Similar Threads
Instalation of Orion Server
Audio in Java
Starting Websphere 6.0 in Eclipse3.3
package javax.servlet.http does not exist
Running Javaflow