What are the best practices for including third-party jar files in Java programs?
I have a program that needs several third-party libraries. At present, its packaging method is as follows:
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 deal with third-party libraries is to put them in the classpath in my jar file list, which will work across platforms, will not slow down startup (bundling them may) and will not encounter legal problems (repackaging may)
The problem lies with users who provide their own third-party libraries (sample use cases, upgrade them to fix errors) Two of these libraries have version numbers in the file, which adds trouble
My current solution is that my program has a boot process that creates a new class loader and uses it to instantiate the program This custom class loader will all in lib / Add the jar file to its classpath
My current method works normally, but I now have two custom class loaders in my application. Recent changes to the code have led to difficult debugging problems, so if there is a better method, I want to eliminate this complexity For me, this is a very common situation, and it seems to be over engineered
So my question is, what should I do?
Solution
We use jar to provide script files For example some. bat,some. SH et al
Starting with Java 6, you can use wildcards to specify Classpaths
This is a good article that explains this approach: https://blogs.oracle.com/mr/entry/class_path_wildcards_in_mustang