How to include external libraries in Ant build.xml file?

In my Java source code, I want to use different classes from java archives (.jar) stored in the "lib" directory of the application. But if I execute "ant run", then I always get the message "java.lang.NoClassDefFoundError". I tried a few things to fix this, but nothing worked ... Maybe someone here can help me?

This is my build.properties file:

app.name=MyApplication app.version=1.0 main.class=mypackage.MyMain build.dir=build classes.dir=${build.dir}/classes jar.dir=${build.dir}/jar dist.dir=dist src.dir=src test.dir=test lib.dir=lib 

This is my build.xml :

 <?xml version="1.0" encoding="UTF-8" ?> <project name="My Project" default="run" basedir="."> <description>My description.</description> <property file="build.properties" /> <path id="classpath"> <fileset dir="${lib.dir}" includes="*.jar"/> </path> <!-- Initialization --> <target name="init" description="Prepare needed directories."> <mkdir dir="${build.dir}" /> <mkdir dir="${classes.dir}" /> <mkdir dir="${jar.dir}" /> <mkdir dir="${dist.dir}" /> <mkdir dir="${lib.dir}" /> </target> <!-- Cleanup --> <target name="clean" description="Remove all files created by the build/test process."> <delete dir="${classes.dir}" /> <delete dir="${dist.dir}" /> </target> <!-- Compile application --> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes" includeantruntime="false"> <!-- <classpath refid="classpath" /> --> </javac> </target> <!-- Java Archive --> <target name="jar" depends="compile"> <!--<delete file="${jar.dir}/${app.name}-${app.version}.jar"/>--> <delete dir="${jar.dir}"/> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${app.name}-${app.version}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Class-Path" value="${lib.dir}"/> <attribute name="Main-Class" value="${main.class}"/> </manifest> </jar> </target> <!-- Run application --> <target name="run" depends="jar"> <java jar="${jar.dir}/${app.name}-${app.version}.jar" fork="true"> </java> <!-- <java fork="true" classname="${main.class}"> <classpath> <path refid="classpath"/> <path location="${jar.dir}/${app.name}-${app.version}.jar"/> </classpath> </java> --> </target> </project> 

It would be nice if anyone could help.

Hooray!

Benny

+6
properties libraries apache ant
source share
3 answers

Replacing classes.dir=${build.dir}/classes with classes.dir=build/classes and jar.dir=${build.dir}/jar with jar.dir=build/jar from your build.properties file will be work.

+3
source share

Change the target jar as follows. I am sure this will work.

 <target name="jar" depends="compile"> <delete dir="${jar.dir}"/> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${app.name}-${app.version}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Class-Path" value="${lib.dir}"/> <attribute name="Main-Class" value="${main.class}"/> </manifest> <zipgroupfileset dir="${lib.dir}"/> </jar> 

+2
source share

I see that you tried to set your class path for the <java> task.

You tried:

 <java jar="${jar.dir}/${app.name}-${app.version}.jar" fork="true"> <classpath refid="classpath" /> </java> 

EDIT: Double check the values ​​in the properties file. Do you have trailing spaces for lib.dir or jar.dir ?

http://ant.apache.org/faq.html#properties-not-trimmed

ant could not create my program through Javak, even when I put the necessary banks in the external build.properties file and refer to them by pathelement or classpath refid.

When ant loads properties from an external file it does not touch the value of the properties, trailing spaces, for example, will not be trimmed.

If the value represents the path to the file as a jar needed for compilation, a task that requires a value, javac for example would not be able to compile, because it cannot find the file due to trailing space.

0
source share

All Articles