3

I looked through the site at all the common posts but my question differs slightly:

What is the best practice for packing a simple Java application that has many other jar files as dependencies?

For example: I have foo.java with a main in it, and foo1.java, foo2.java that are accessed from foo.java. And I am using log4j.jar, mysql.jar on my eclipse build path.

Right now I am using ant, which works well to build it. And what I do is write a simple .sh script that references all the classpath and log4j info. But this means I have to give them all those jars and they have to be in the right location. I want to be able to say "java -jar foo.jar" and have it run on any machine without having to transfer any other files.

Maybe a .jar is not the best way to go. I just want to be able to give one file to someone, who does not know how to setup a class path and everything, and have it able to run.

Also I am curious as to what the best practice is. Do you usually just give someone a jar and give them a zip of all the dependency jars and tell them to put it on the class path?

Do you somehow make a .rpm?

I am not familiar with MAVEN, but if that is the best way, I will do a tutorial. Right now I use ant.

chantheman
  • 5,256
  • 4
  • 23
  • 34

4 Answers4

2

You can merge multiple jar-files into a single jar file using tools such as

Then you can start your application using a simple jar -jar yourApplication.jar.

From the webpage of OneJar:

What is One-JAR?

One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.

Both JarJar and OneJar have Ant-tasks for integration with Ant included in their distributions.


Another option is to use WebStart. This way all dependencies are downloaded automatically, and rolling out new versions is a breeze. Requires web-access on the initial run though.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2

Personally I don't like dumping all dependencies into a single jar file like this. This makes it difficult for people looking at the binary distribution to figure out what the program really depends on.

What I prefer to do is to create a lib directory with my jar and all its dependencies. Specify the classpath with Class-Path: in the manifest.mf. Specify the main class with Main-Class: in the manifest. Then use java -jar my.jar to run the application. You simply need to pack up your class and all its dependencies in a zip or tar.

Maven does have a task to automate manifest creation, and one to automate archive creation. But, for a simple project with a single artifact and 3rd party libs that rarely change, its easy to build up in an ant script.

Mike Miller
  • 2,149
  • 1
  • 18
  • 23
  • Awesome. Okay so I have a dir called 3rdParty that I put everything in. So if I make a manifest file that contains the class path and then zip up all those jars and the new jar, I should be able to give someone that and let them unzip it.... cool. – chantheman Mar 24 '11 at 20:29
  • Correct, I usually add a shell/batch script to run the program, but that is mostly tradition. With -jar you literally just need the command I have above. – Mike Miller Mar 24 '11 at 20:31
  • For reference: you want the maven-shade-plugin if you're going to smush jars together. – Dominic Mitchell Mar 25 '11 at 13:25
  • Oh nice. Okay. Yea I keep hearing about maven, but everyone I work with is using ant, so I think I am stuck. – chantheman Mar 26 '11 at 04:11
  • The *manifestclasspath* task can be used to generate the correct class path within the jar. See this answer: http://stackoverflow.com/questions/3796824/ant-how-to-get-all-files-name-in-a-specific-folder – Mark O'Connor Mar 30 '11 at 19:31
0

Try Spring Boot. It will package all your JARs into single fat executable JAR, which can be simply executed using java -jar myjar.jar Also it gives you a lots of other cool features coming with Spring. Check Spring docs: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-executable-jar

stinger
  • 3,790
  • 1
  • 19
  • 30
0

You can use tools like JarJar to automatically bundle all dependencies into a single JAR file, so your users need just one file and can do java -jar foo.jar (or double-click on it).

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87