I'm new to Ant buildfiles, and I've managed to set up my buildfile to create my build directory structure, compile all my files, and jar them into an jar with a manifest specifying the main class correctly.
However, I'm also trying to put into the buildfile the capability to run the jar I just created. It will attempt to run, but I encounter this error:
run:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: edu/course/lab/pkg3/Lab31
[java] at edu.school.oad.lab.pkg1.Main.<init>(Unknown Source)
[java] at edu.school.oad.lab.pkg1.Main.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: edu.course.antlab.pkg3.Lab31
[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] ... 2 more
[java] Java Result: 1
So I know this means that when I try to run the jar file, the jar file is structured properly and the manifest is set up correctly, but it cannot find the class Lab31 because it is not included in that particular jar file. Instead, it is contained within another jar file located in my library folder, ./lib/ . I tried using the same method to set the classpath for running the jar file the same way that I set the classpath for the actual compilation, but that doesn't seem to be working.
Here's what I have for the run portion of the buildfile:
<target name="run" depends="init, prepare">
<java jar="${build}/jar/AntLabRun.jar" fork="true">
<classpath>
<pathelement location="${lib}/resources.jar" />
</classpath>
</java>
</target>
My only thought would be that the jar file is thinking the resources.jar file is located at ${build}/jar/${lib}/resources.jar instead of just ${lib}/resources.jar, but if that's the case I'm still not sure how to fix it. Can anyone offer some guidance?
The java task probably behaves the same way as the java command line. When both jar and classpath options are given, the classpath is ignored, instead the jar is expected to contain a manifest listing the needed libraries. I would suggest removing the jar attribute, specifying both jars as classpath elements and adding the classname
attribute to the java task with the name of the main class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With