I'm trying to find a way to dynamically load a platform dependent library jar for the current OS. I need to access a third party library that uses JNI calls, and has different jars for Mac and Windows. All of the calls and returns for the two libraries are identical, and simply swapping out the jar file in the lib dir after compiling works, but I would like my app to be able to determine which jar should be loaded at runtime.
I have found similar posts on here, but nothing that addresses this question directly. this post shows how to dynamically load jars via the ClassLoader, but from what I gather this means I would have to use the Method method = clazz.getDeclaredMethod("methodName")
, method.invoke()
approach to call any methods provided by that class, as well as explicitly call out each class to be loaded to the class loader (which I would very much like to avoid).
I'm hoping to find a solution that looks something like:
import external.lib.namespace.LibClassA;
import external.lib.namespace.LibClassB;
public class MyClass {
static {
if (System.getProperty("os.name").contains("Windows") {
ClassLoader.getSystemClassLoader().load("Lib_win.jar");
} else {
ClassLoader.getSystemClassLoader().load("Lib_mac.jar");
}
}
public static void Main(String[] args) {
LibClassA.platformDependantCall();
LibClassB.someOtherCall();
}
}