I know this question has been solved but none of the answers solve my problem. Also I am new in this. I do get NoClassDefFoundError: org/apache/commons/lang3/StringUtils
I can see it resolves the dependencies but when I run the jar after compilation i get class not found.
Here is my ivy.xml file
<configurations>
<conf name="runtime" description="Runtime"/>
</configurations>
<dependencies>
<dependency org="org.apache.commons" name="commons-lang3" rev="3.6" conf="runtime->default"/>
<dependency org="junit" name="junit" rev="4.12" conf="runtime->default"/>
</dependencies>
and following is my build.xml
<project name="HelloWorld-build" basedir="." default="clean-deploy" xmlns:ivy="antlib:org.apache.ivy.ant">
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<target name="init" description="resolve dependencies with ivy">
<ivy:resolve />
<ivy:cachepath pathid="default.classpath" conf="runtime" />
</target>
<target name="compile" depends="init">
<mkdir dir="${build.dir}/classes" />
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false">
<classpath refid="default.classpath" />
</javac>
</target>
<target name="package" depends="compile">
<mkdir dir="${dist.dir}" />
<jar destfile="${dist.dir}/HelloWorld.jar" basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="mypackage.HelloWorld" />
</manifest>
</jar>
</target>
<target name="deploy" depends="package">
<java jar="${dist.dir}/HelloWorld.jar" fork="true">
</java>
</target>
</project>
I got problem when I run deploy target. When I run my java application manually (not using build.xml) then it works fine. Please help me solving this issue.
And this is java file
import org.apache.commons.lang3.StringUtils;
public class HelloWorld {
public static void main(String[] args) {
String message = "Hello world!";
if (!StringUtils.isEmpty(message)) {
System.out.println(message);
}
}
}
NoClassDefFound error is thrown as the commons-lang3 is not available in the runtime classpath. You have given it only in the javac as a classpath which means it is available only in the compile stage.
Since this is an external jar not packaged with your jar, it has to be available in the runtime claspath. But when using the jar attribute, all classpath settings are ignored by Ant.
To resolve this, you can change your deploy target as below
<target name="deploy" depends="package">
<java classname="mypackage.HelloWorld" fork="true">
<classpath>
<path refid="default.classpath"/>
<path location="${dist.dir}/HelloWorld.jar"/>
</classpath>
</java>
</target>