Compilation successful, NoClassDefFoundError in ant java task
I already have many hours on the internet and have read many similar questions like this and. I've now simplified my code to a few short lines, so the problem should be obvious - but I still can't seem to solve it.
Here are my two simple classes:
package ant;
import my.package.util.exception.UtilTest;
public class Debug {
public static void main(String[] args) {
System.out.println(UtilTest.myTest());
}
}
package my.package.util.exception;
public class UtilTest {
public static boolean myTest(){
return true;
}
}
I built my.package.util.jar and put it in the lib directory under the project directory. Now I ran the ant built script (see below) to build the jar for the Debug class. Built is sufficient, MANIFEST.MF:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_20-b26 (Oracle Corporation)
Main-Class: ant.Debug
Class-Path: lib/my.package.util.jar
with an empty line at the end.
Here is the build.xml file:
<?xml version="1.0"?>
<project>
<path id="my.classpath">
<fileset dir="lib/" includes="**/*.jar"/>
<pathelement location="."/>
<pathelement location="bin"/>
</path>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" encoding="UTF-8">
<classpath refid="my.classpath"/>
</javac>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Debug.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="ant.Debug"/>
<attribute name="Class-Path" value="lib/my.package.util.jar"/>
<!-- EDIT: to solve the issue, use value="../../lib/my.package.util.jar"/-->
</manifest>
</jar>
</target>
<target name="debug">
<java classpath="${my.classpath}" jar="build/jar/Debug.jar" fork="true">
<arg value="${file}"/>
</java>
</target>
</project>
By running ant debug "I get:
debug:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: my/package/util/exception/UtilTest
[java] at ant.Debug.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: my.package.util.exception.UtilTest
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 1 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second
Who can help?: -/
Any hints are appreciated!
source to share
The problem has been resolved.
I finally figured out that I need to set the relativ path of the destination jar file path, not relative to build.xml. So I have to add ../../ before the classpath.
<manifest>
<attribute name="Main-Class" value="ant.Debug"/>
<attribute name="Class-Path" value="../..lib/my.package.util.jar"/>
</manifest>
source to share