When I run \"clean and build\" the .jar
file that is being created only runs if the lib
folder is at the same folder of the .jar
file.
If you are going to distribute your app to another pc
You just zip .jar along with lib folder.
If want to run your app from any place in your pc
Take in cosideration Maven way of doing this - create local repository eg. C:\libs where your libraries would exist and .jar could accesses them later from any place in your pc.
Or you could just use Maven. There is a discussion on distributing application with all dependencies (libraries): Java: How do I build standalone distributions of Maven-based projects?.
I have found maybe the easiest solution for this problem here. You just need to copy the next code snippet at the end of the build.xml
file in your project folder.
<target name="-post-jar">
<!-- Change the value to the name of the final jar without .jar -->
<property name="store.jar.name" value="MyJarName"/>
<!-- don't edit below this line -->
<property name="store.dir" value="dist"/>
<property name="temp.dir" value="temp"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${temp.dir}"/>
<mkdir dir="${temp.dir}"/>
<jar destfile="${temp.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<delete dir="${store.dir}"/>
<zip destfile="${store.jar}">
<zipfileset src="${temp.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete dir="${temp.dir}"/>
</target>
Go to the build.xml in the root of your project and add the code right before </project>
tag at the end.
Now change the value of the first propertiy field as commented and save the changes.
From now on, each time you Clean & Build your project, the full jar with dependencies will be generated in the project dist folder
You can also use this (when the libraries are not in "dist/lib"), tested with NetBeans and ant-contrib:
<target name="-post-jar">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<!-- Path to ant-contrib -->
<pathelement location="../../Libs/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!-- Change the value to the name of the final jar without .jar -->
<property name="store.jar.name" value="${application.title}"/>
<!-- don't edit below this line -->
<property name="store.dir" value="dist"/>
<property name="temp.dir" value="temp"/>
<property name="temp.libs.dir" value="temp/libs"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<fileset id="store.jars.absolute" dir="${store.dir}" includes="*.jar"/>
<pathconvert property="store.jars.relative" refid="store.jars.absolute" dirsep="/">
<map from="${basedir}/" to=""/>
</pathconvert>
<for list="${store.jars.relative}" param="item">
<sequential>
<echo message="Adding @{item} into single JAR at ${store.jar}"/>
</sequential>
</for>
<delete dir="${temp.dir}"/>
<mkdir dir="${temp.dir}"/>
<for list="${javac.classpath}" param="item" delimiter=":">
<sequential>
<echo message="Adding @{item} into single JAR at ${store.jar}"/>
<copy file="@{item}" todir="${temp.libs.dir}" overwrite="true" />
</sequential>
</for>
<jar destfile="${temp.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="${store.dir}" includes="*.jar"/>
<zipgroupfileset dir="${temp.libs.dir}" includes="*.*"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<delete dir="${store.dir}"/>
<zip destfile="${store.jar}">
<zipfileset src="${temp.dir}/temp_final.jar" excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete dir="${temp.dir}"/>
</target>
user1016588's solution works for me. There's one typo: this line should be zipfileset src="dist/lib/commons-io-1.4.jar" excludes="META-INF/*"
That's really easy to package every dependent library (*.jar) into one single myProject.jar.
Just follow these steps and you will finally pack every dependent library into single jar. If you are using NetBeans then you can follow exactly or else you need to find your build.xml file in project files.
Follow these steps to edit build.xml
1) Click on Files tab
on the left side of the project panel in NetBeans.
2) Double click on the build.xml
file and add these lines in it just before </project>
line
<target name="package-for-store" depends="jar">
<property name="store.jar.name" value="myProject"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>
3) Change value
in second line of the code as per your project name which is
<property name="store.jar.name" value="myProject"/> //<---Just value not name
4) Save it and right click on build.xml and choose Run Target and then Other Targets and finally click on Package-for-store
5) And here you done. Now you can go and check just like dist
folder there will be a store
folder which will be containing your final complete jar including all of your dependent libraries. Now whenever you want to change / add more libraries or so, just follow step 4.
Picture for step 4
You could use Apache Ant since version 1.7 for build the JAR with the required libraries in only one file. You could have a configuration file as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="buildJar">
<target name="buildJar">
<!-- Name of jar -->
<jar destfile="C:/MyJar.jar" filesetmanifest="mergewithoutmain">
<manifest>
<!-- Your class with the main method -->
<attribute name="Main-Class" value="myPackage.MyClass"/>
<!-- Path in the jar -->
<attribute name="Class-Path" value="."/>
</manifest>
<!-- Dir of compiled class -->
<fileset dir="C:/NetBeansProjects/MyProject/bin"/>
<!-- Include required jars -->
<zipfileset excludes="META-INF/*.SF"
src="C:/NetBeansProjects/MyProject/lib/library1.jar"/>
<zipfileset excludes="META-INF/*.SF"
src="C:/NetBeansProjects/MyProject/lib/library2.jar"/>
</jar>
</target>
</project>
In Netbeans, place the XML file in your project and run it with the context menu.
See more in Apache Ant User Manual.