简体   繁体   中英

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.

PS 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!

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.

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

Yes unpacking and packing will irritate you sometimes especially in Linux environments like Ubuntu,

Before unpacking make sure your Java_Home and path is exported to the folder as below: In your home directory, you will most likely have a file called.bashrc. Go to the end of the file and add the following

export JAVA_HOME=<path to your JDK install e.g. /opt/jdk>
export PATH=$JAVA_HOME/bin:$PATH

This sets your JAVA_HOME environment variable - commonly used by Java-based applications. It then prepends that directory to your PATH variable. The operating system understands PATH as a list of the parent directories of executable files.

Now unpack jar xvf JAR_NAME.jar Packing back jar cf JAR_NAME.jar *

Have a look at jar jar . It sounds like it will do what you need.

Have you tried "Export runnable jar" in eclipse? It should works for you.

Take a look at this picture:图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM