Java Packaging/Building Common Techniques Jar File Specifically
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 开发者_Python百科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.
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.
You can merge multiple jar-files into a single jar file using tools such as
- FatJar
- OneJar
- JarJar
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.
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).
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
精彩评论