Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for including third party jar files in a Java program?

I have a program that needs several third-party libraries, and at the moment it is packaged like so:

zerobot.jar (my file)
libs/pircbot.jar
libs/mysql-connector-java-5.1.10-bin.jar
libs/c3p0-0.9.1.2.jar

As far as I know the "best" way to handle third-party libs is to put them on the classpath in the manifest of my jar file, which will work cross-platform, won't slow down launch (which bundling them might) and doesn't run into legal issues (which repackaging might).

The problem is for users who supply the third party libraries themselves (example use case, upgrading them to fix a bug). Two of the libraries have the version number in the file, which adds hassle.

My current solution is that my program has a bootstrapping process which makes a new classloader and instantiates the program proper using it. This custom classloader adds all .jar files in lib/ to its classpath.

My current way works fine, but I now have two custom classloaders in my application and a recent change to the code has caused issues that are difficult to debug, so if there is a better way I'd like to remove this complexity. It also seems like over-engineering for what I'm sure is a very common situation.

So my question is, how should I be doing this?

like image 938
ZoFreX Avatar asked Mar 13 '10 03:03

ZoFreX


People also ask

Where are third party JARs placed?

Lib. Include all third party libraries to be used on your dynamic plugin to the lib folder under the bundle's root directory.

What is third party library in Java?

A third party library refers to any library where the latest version of the code is not maintained and hosted by Moodle. An example is "Mustache. php".

What are JAR files used for in Java?

JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.

What should JAR files be associated with?

A JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform.


1 Answers

We provide script files with the jar. E.g. some.bat, some.sh etc.

And as of Java6, you can use wildcard to specify classpaths.

Here is a good article that explains this approach : https://blogs.oracle.com/mr/entry/class_path_wildcards_in_mustang

like image 120
Enno Shioji Avatar answered Oct 24 '22 06:10

Enno Shioji