File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Ant, Maven and Other Build Tools and the fly likes [Ant] Run a jar file throw java.lang.NoClassDefFoundError: app/main/Main and Could not find the main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "[Ant] Run a jar file throw java.lang.NoClassDefFoundError: app/main/Main and Could not find the main" Watch "[Ant] Run a jar file throw java.lang.NoClassDefFoundError: app/main/Main and Could not find the main" New topic
Author

[Ant] Run a jar file throw java.lang.NoClassDefFoundError: app/main/Main and Could not find the main

Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
I have a simple sample to get started with Ant such as show a Hello frame.
My project structure is:



And ant.xml file like that:



It ran OK until target "run" when this error is thrown out:
Buildfile: D:\workspaces\Java_app\antSample\ant.xml
run:
[java] java.lang.NoClassDefFoundError: app/main/Main
[java] Caused by: java.lang.ClassNotFoundException: app.main.Main
[java] at java.net.URLClassLoader$1.run(Unknown Source)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
[java] Could not find the main class: app.main.Main. Program will exit.
[java] Exception in thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 352 milliseconds

I have try to modify Ant Runtime as some intructions found on internet
Window > References > Ant > Runtime > Point to Ant Home to /eclipse_home/plugins/org.apache.antXXXX
Add tool.jar from JDK

But it's still getting stack.

Thanks for anysuggestion :-)
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

Is the file hierarchy you posted correct, or is there a typo? Specifically, is the Main class source at antSample/app/main/Main.java or at antSample/app.main/Main.java? Please post the contents of the JAR file by running "jar -tf path/to/antSample.jar"

You didn't show enough of the build.xml. What is ${mainclass}? Where are you compiling your source files to? It is that directory that you should use for <jar ... basedir="xxx"> (the xxx should be the directory into which the class files were compiled, not ${basedir}).




JBoss In Action
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Peter Johnson wrote:Is the file hierarchy you posted correct, or is there a typo? Specifically, is the Main class source at antSample/app/main/Main.java or at antSample/app.main/Main.java? Please post the contents of the JAR file by running "jar -tf path/to/antSample.jar"

You didn't show enough of the build.xml. What is ${mainclass}? Where are you compiling your source files to? It is that directory that you should use for <jar ... basedir="xxx"> (the xxx should be the directory into which the class files were compiled, not ${basedir}).


Thanks for your reply.
Here is the hierarchy


This is the contents of the JAR file


And this is ant content


And all the sample I've uploaded here
http://www.mediafire.com/?cm345ypfht6hsdf


Thanks for your support :-)
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

Hmm, what you have works for me. My Main class only does a System.out.println(). And I do this from the command line:



Does Main.java rely on any classes in a JAR in the lib directory? If so, then it could be that those classes not being available is what is causing the problem. (When you pass a JAR to java to run, java ignores the classpath.) Could you post the contents of Main.java?

Also, you really should have "depends" declarations in your <target>s so that "run" depends on "jar" which depends on "compile".
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26


Yes, I want to use an external library.
But it even did not work with "System.out.println()"
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

Read this sentence again:

When you pass a JAR to java to run, java ignores the classpath.


This means that in your case adding the JXL JAR to the classpath does no good - it will not find those classes. You would need to either include the JXL classes in your JAR, or change the <java> task to run a class instead of a jar.


But it even did not work with "System.out.println()"

It would have helped if you had provided that code. Did it still include jxl.Workbook? Or BiffException?

But I still think there is something else going on.

Does the version that only has a println, no with jxl nor swing run from the command line after building it: java -jar target/antSample.jar

Are you running ant from the command line of from within an IDE? If from within an IDE, try running from the command line. If that works, then you misconfigured the IDE to run ANT.

If none of those help, add change the <java> task to include a jmvarg entry:



Post the output from running with that option.
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
I found the problem that does not concern Ant
I have a simple Main.java as below


Stand at D:\workspaces\Java_app\antSample\src\app\main that contains the file Main.java



Remove package app.main; in the source then build and run again



So what is the problem here

By the way, would you mind giving a simple ant with external library
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

The problem is that you don't understand how packages relate to classpaths. You should have cd'ed to the D:\workspaces\Java_app\antSample\src directory and then run "java app.main.Main", then it would have found the class. (Note that the javac compiler deals with files within directories, whereas the java runtime deals with classes within classpaths, and packages are part of the class name.)

But that is not the same issue you ran into with Ant because the way your build script is set up, doing "ant compile jar run" works for me.
Phuc Bui
Greenhorn

Joined: Feb 19, 2009
Posts: 26
Peter Johnson wrote:The problem is that you don't understand how packages relate to classpaths. You should have cd'ed to the D:\workspaces\Java_app\antSample\src directory and then run "java app.main.Main", then it would have found the class. (Note that the javac compiler deals with files within directories, whereas the java runtime deals with classes within classpaths, and packages are part of the class name.)

But that is not the same issue you ran into with Ant because the way your build script is set up, doing "ant compile jar run" works for me.


Actually, I do not understand ant very well so I have returned to simple ant and it worked :-)
Thanks for your support, I will try more complex ant
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: [Ant] Run a jar file throw java.lang.NoClassDefFoundError: app/main/Main and Could not find the main
 
Similar Threads
abstract class
Jar issue - NoClassDefFoundError when attempting to run. Class-Path set in MANIFEST.MF
Jnlp cannot find the main class in a Jar packaged using "OneJar".
ClassNotFoundException: com.mysql.jdbc.Driver
Ant: Compile time vs runtime classpath