Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I repack a jar file with all of its dependencies?

I am developing an application which is supposed to run standalone. However, this project involves a .jar file which contains a lot of dependencies, and if I simply distribute this .jar file with the application, it won't work.

I wonder if there is any way in which I could unpack the file, add the dependencies and repack it again? I hope there are some automatic mechanism for this, since the manual process could take hours, and there might be other referenced jar files.

P.S. I am using Eclipse, but since I am going to deploy this project with Web Start, exporting the project with the build-in export tool might not be a good idea since my attempts all ended up with ClassNotFoundException, so I suspect I might have to pack the project into several jars.

Thanks!

like image 637
zw324 Avatar asked Jul 08 '11 19:07

zw324


2 Answers

Repacking an unpacked JAR is a little frustrating because of the folder structure

When unpacking with:

jar xvf JAR_NAME.jar

you get a JAR_NAME/ folder

To repack the JAR:

  • remove old jar

    rm JAR_NAME.jar

  • get inside the folder

    cd JAR_NAME

  • pack the jar referencing the parent folder

    jar cf ../JAR_NAME.jar *

and you will end up with the JAR_NAME.jar in the parent folder, where the original was unpacked from, without the first folder level you would get if you had packed the folder itself.

like image 91
MrE Avatar answered Nov 15 '22 19:11

MrE


For Spring Boot 2.1.9.RELEASE I managed to unpack and re-pack the fat jar like this: Move into the folder with the jar file.

Extract jar file

jar xvf app.jar

Remove old jar file

rm app.jar

Create new jar file in parent dir

jar cmf0 META-INF/MANIFEST.MF ../app.jar  *

m ... use a specific manifest-file

0 ... deactivates compression, which circumvents that the dependencies(jar-files) are compressed

Documentation can also be found here: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

like image 37
anstue Avatar answered Nov 15 '22 19:11

anstue