I am trying to build a project using Ant Build and i have referenced several jars to make it work. Now When i put the jar created by Ant build in some other machine and run it.
I am getting the error saying NoClassDefFoundError org/apache... Not found
.
Is there anyway to put all the referenced jars in the classpath of the project or in the manifest file?
Or is there anyway to repackage all the jar in a project?
I know there is one method using jarjar but I don't have any idea of how to use it.
Please suggest me some ideas, I've been stuck with this small problem for a long time.

- 1,686
- 3
- 29
- 43

- 366
- 1
- 12
-
How are you running it? – mmmmmm Apr 17 '13 at 12:51
-
Maybe this will help: http://stackoverflow.com/questions/1821803/creating-a-bundle-jar-with-ant – NeplatnyUdaj Apr 17 '13 at 12:51
-
Are the external jars (the one's containing org/apache, etc.) on the destination machine? Is the classpath or java.ext.dirs set to point to the external jars on the destination maching? – karakuricoder Apr 17 '13 at 12:52
-
*"Pls Suggest some ideas"* Spell words properly is one idea - that word is 'please'. – Andrew Thompson Apr 17 '13 at 12:52
-
I have created one folder named lib and added all the referenced jars in that. – Karthik Venkateswaran Apr 17 '13 at 12:58
-
Actually i am modifying the source code of a certain project to suit to my scenario. When i replace the old jar with my jar i am getting the error. – Karthik Venkateswaran Apr 17 '13 at 13:00
-
It is still not specific enough. There are important things that we don't know. How do you think we could solve your problem? – gaborsch Apr 17 '13 at 13:40
1 Answers
When building with ANT the external jars you need are being added to the classpath by the ANT tool. Look in your build script, you most likely have an entry either in the javac task or in a setup task that defines your classpath.
After you build your code, your jar file only has your classes in it, the classes in the 3rd party jar files (like Apache) are not added to your jar file by default.
What you need to decide is do you want a single jar file with all needed classes or are you willing to deploy multiple jar files? If you're comfortable delivering your application as multiple jar files, you will want to provide a batch file or shell script to launch the application for the user that builds the classpath to include the deployed jars.
If you want one jar file, you can do something like below. Assume that all the 3rd party jars you have are in a directory identified by the ANT property lib.dir:
<jar destfile='${build.dir}/lib-jars.jar'>
<zipgroupfileset dir="${lib.dir}">
<include name="**/*.jar" />
</zipgroupfileset>
</jar>
<sleep seconds='1'/> <!-- avoid timestamp warnings -->
what this will do is create in your build.dir directory a single jar file named lib-jars.jar that contains all the classes from all of the 3rd party jars. Understand that this will result in equivalent files (like MANIFEST.MF files) being overwritten if they exist in multiple jars and only the last one will be present.
Once you have this new all-libs jar, you can then jar up your application classes and the contents of this all-libs jar into one jar:
<jar destfile='${jar.file}' basedir='${classes.dir}'>
<!-- using zipfileset we can filter on entries in the one file -->
<zipfileset src='${build.dir}/lib-jars.jar'>
<exclude name="META-INF/MANIFEST.MF"/>
</zipfileset>
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="${main.class}"/>
<section name="common">
<attribute name="Specification-Title" value="${project.title}"/>
<attribute name="Specification-Version" value="${release.version}"/>
<attribute name="Specification-Vendor" value="${vendor}"/>
<attribute name="Implementation-Title" value="${project.title}"/>
<attribute name="Implementation-Version" value="${release.version}"/>
<attribute name="Implementation-Vendor" value="${vendor}"/>
</section>
</manifest>
</jar>
notice that I exclude the MANIFEST.MF file from the all-libs jar and create my own. Then end result of this is a single jar file that includes all of the classes/property files/resources from all of the library jar files and your classes.

- 581
- 2
- 3