How to build a single JAR file with external libs
How to combine external libs (.jar files) in an own single JAR file.
If you build a JAR file with NetBeans and your project includes external (or own) compile – time libraries, the final JAR files requires the lib folder with the external JARs in the same directory. For some reasons it’s necessary to have just a single JAR file which everything includes.
Sun (Oracle) describes at Use NetBeans IDE 6.7 to Combine JAR Files Into a Single JAR File how to do that.
In a nutshell:
Copy following source code into the <project> tag, right before </project> closing tag.
<target name="package-for-store" depends="jar"> <!-- Change the value of this property to be the name of your JAR, minus the .jar extension. It should not have spaces. <property name="store.jar.name" value="MyJarName"/> --> <property name="store.jar.name" value="MarsRoverViewer"/> <!-- don't edit below this line --> <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>
Edit the attributes name and value.
<property name="store.jar.name" value="MarsRoverViewer"/>
Run the target as package-for-store and in the project folder a “store” folder appears. You can find the final TAR file in this folder now.
Greetz Mavi
Categories: General
development, java, netbeans
Thank you for your tips. They are very useful!! My interest in IT is growing with the minute and to have such a great channel just enlarges it. I am thinking of starting one myself..I will keep you updated.
Oh and I heard from a friend that the paste you bought for repairing mobile phone screens, which turns out not to be working, is also a great hairgel. Means you did not buy it completely for nothing! Give it a go I would say!
greetz,
jwv
Maybe worth noting that you must edit the “build.xml” file. You never mentioned the name of the file to add this text to.
Thank You brother it helps me a lot. no more lib folder
while including lib into jar file some of the jar of the lib folder not included into the packaging of jar file. please help why this is happening and what the solution.
Thank you very much, helped out at the right time!
taskdef class net.sf.launch4j.ant.Launch4jTask cannot be found
Very good info. Lucky me I discovered your site by accident (stumbleupon).
I have book marked it for later!
I lie tһe heelpful information you provide in your articles.
I’ll bookmark уour blog and check aցain heгe frequently.
Ӏ’m գuite certаіn I will learn plenty of new stuff rіght heге!
Good luck for the next!