What is the best practice of 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 this:

 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 libraries is to put them in the classpath in the manifest of my jar file, which will work cross-platform, will not slow down the launch (this union they can) and do not encounter legal problems (which may be repackaged).

The problem is that users who themselves provide third-party libraries (use case, updating them to fix the error). Two libraries have a version number in the file, which adds to the hassle.

My current solution is that my program has a boot process that creates a new class loader and runs the program using it. This custom classloader adds all .jar files in lib / to its classpath.

My current method works fine, but now I have two custom class loaders in my application, and a recent code change has caused problems that are difficult to debug, so if there is a better way, I would like to remove this complexity. It also seems like over-designing as I am sure this is a very common situation.

So my question is: how do I do this?

+7
java classpath jar dependencies packaging
source share
4 answers

We provide script files to the bank. For example. some.bat, some.sh, etc.

And with Java6, you can use a wildcard to indicate the class path.

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

+5
source share

If your audience is technical (and it sounds like they are ready to drop new jar files), could you possibly provide .sh and .bat files that they can edit to change the class path? This will be more transparent to them than a custom class loader.

+1
source share

You can try Fat-Jar solution, it works fine with Fat Jar Eclipse Plug-In . I used for several projects without any problems. Its principle seems to coincide with your current solution.

0
source share

I think I'm going to go manifest. Declare the class path in the manifest for the jar entry and enter the entries for:

 libs / pircbot.jar
 libs / mysql-connector-java-5-bin.jar
 libs / c3p0.jar

Then, if users want to upgrade to the latest versions of the library, they will have to rename them according to what is declared in the manifest. I don’t think this is too big a problem, and it makes things a lot easier inside.

I also just don't like the idea of ​​loading everything in. / Lib / automatically, which seems potentially dangerous.

0
source share

All Articles