• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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}).



 
Phuc Bui
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes, I want to use an external library.
But it even did not work with "System.out.println()"
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
I can't take it! You are too smart for me! Here is the tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic