10

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!

Community
  • 1
  • 1
zw324
  • 26,764
  • 16
  • 85
  • 118
  • 2
    possible duplicate of [Java: Easiest way to merge a release into one jar-file](http://stackoverflow.com/questions/81260/java-easiest-way-to-merge-a-release-into-one-jar-file) – Robin Jul 08 '11 at 19:22
  • @Robin In this case the jar is not runnable. However, I believe the problem is very similar. But with answers, I cannot delete my question now. – zw324 Jul 08 '11 at 19:25
  • Runnable or not, merging the jar files is the same thing, just no main class declared. – Robin Jul 08 '11 at 19:33
  • Please note that in the case that several original jar files contain the same filename with different content (mostly content of META-INF, like MANIFEST.MF or Spring's schema handler declaration), these files need to be merged somehow, or else you might get strange errors. – Christian Semrau Jul 08 '11 at 19:42

5 Answers5

23

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.

MrE
  • 19,584
  • 12
  • 87
  • 105
6

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

anstue
  • 990
  • 12
  • 24
3

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 backjar cf JAR_NAME.jar *

HariKishore K
  • 399
  • 3
  • 6
1

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

thunderflower
  • 181
  • 1
  • 5
  • Um... Jar-Jar Links... Being a Die-hard Star Wars fan, I serious doubt anything with such a name can function properly. Just kidding, will check:) – zw324 Jul 08 '11 at 19:23
  • Star wars aside: I've actually used it professionally. It is quite established/mature. – thunderflower Jul 08 '11 at 19:30
0

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

Take a look at this picture: picture

KrzyH
  • 4,256
  • 1
  • 31
  • 43
  • Yes but I had problems when I tried to deploy the project (see the P.S. part), so I gave up this method. – zw324 Jul 08 '11 at 19:24
  • I forgot to mention in the previous comment that the jar file I am trying to repack is not runnable:( – zw324 Jul 08 '11 at 19:27