Java Packaging / Building Common Techniques Jar File Specific

I looked at the site on all common posts, but my question is slightly different:

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

For example: I have foo.java with the main one in it and foo1.java, foo2.java, which are accessed from foo.java. And I use log4j.jar, mysql.jar in my eclipse build path.

Now I am using ant, which works well to create it. And what I am doing is writing a simple .sh script that references the entire classpath and log4j information. But that means I have to give them all these banks, and they should be in the right place. I want to be able to say "java -jar foo.jar" and run it on any computer without having to transfer any other files.

Maybe .jar is not the best way. I just want to provide someone with a single file that does not know how to configure the class path and all that, and be able to run it.

I am also curious about what is the best practice. Usually you just give someone a can and give them the mailbox of all the cans with the dependencies and tell them to put it in the class path?

Do you somehow do .rpm?

I am not familiar with MAVEN, but if this is the best way, I will do a tutorial. Now I am using ant.

+3
source share
4 answers

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

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

Maven has the task of automating the creation of the manifest, as well as automating the creation of the archive. But for a simple project with one artifact and third-party libraries that rarely change, it is easy to create it in an ant script.

+2
source

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

You can then launch your application using the simple jar -jar yourApplication.jar .

From the OneJar webpage:

What is a One-JAR?

One-JAR allows you to pack a Java application along with its dependency bars into a single Jar executable.

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


Another option is to use WebStart. Thus, all dependencies are loaded automatically, and the deployment of new versions is a breeze. Web access required on first start.

+2
source

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

0
source

Try Spring Download . It will pack all your JAR files into a single-core executable JAR, which you can simply execute using java -jar myjar.jar It also gives you many other interesting functions available with Spring. Check out the Spring docs: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-executable-jar

0
source

Source: https://habr.com/ru/post/906182/


All Articles