jpype icon indicating copy to clipboard operation
jpype copied to clipboard

Change java.library.path in runtime

Open KOLANICH opened this issue 3 years ago • 4 comments

It'd be nice to be able to change (append dirs, at least) java.library.path in runtime after JVM was launched. I have tried some recepies from stackoverflow, they are incompatible to the loader used. It may make sense to modify the loader to support this feature explicitly without using the hacks from SO.

KOLANICH avatar May 26 '21 20:05 KOLANICH

Can you point me to recipes? I can add support to DynamicClassLoader if it is something reasonable. We can do run time changes to class path, but I have not tried to make the library path dynamic.

Thrameos avatar May 26 '21 21:05 Thrameos

https://stackoverflow.com/questions/15409223/adding-new-paths-for-native-libraries-at-runtime-in-java

KOLANICH avatar May 26 '21 21:05 KOLANICH

Err, these are really very brutal hacks that are getting in and messing with the private variables of the JVM. There is both System.loadLibrary and System.load.

The intent of loadLibrary is to only load libraries that predefined as safe. The intent of load is to take an absolute path. Our DynamicClassLoader can of course trigger a load or a loadLibrary if that is required.

https://www.pixelstech.net/article/1549365534-The-difference-between-System-load-and-System-loadLibrary-in-Java

Can you give me an example of what you are trying to do? I assume that you have a jar file with a shared library in some directory and you would like to load both upon request. Rather than hacking the path using private variables we can just create a call that does the required work using the absolute method. Unfortunately I am not sure what triggers the loadLibrary call. It may be an initializer in the jar file in which case we would need to use asm or some other means to intercept the call. Perhaps the security manager can do it. That is if they were kind enough to define an extension hook. In some places Java prevents pretty much any reasonable method. But I would need to start with an example so that I can see the mechanism.

Alternately I have at times used the fact that shared library loads are joined from C and Java such that I could trigger a shared library load in C which actually caused Java to find the symbols it needed. Though I am not sure how portable that solution actually is.

Thrameos avatar May 26 '21 21:05 Thrameos

I assume that you have a jar file with a shared library in some directory and you would like to load both upon request. Rather than hacking the path using private variables we can just create a call that does the required work using the absolute method.

Not quite that. There is a jar, that I use as an almost black box. That jar internally loads a native library. In order this to work, the native library should be available in java.library.path ...

An example of such an app is DocFetcher. Prebuilt binaries can be downloaded from https://sourceforge.net/projects/docfetcher/files/latest/download . https://github.com/KOLANICH-tools/docfetcher/blob/jpype/dist/search.py is the script (they used to be using py4j, I have rewritten to work with jpype). It depends on a native lib jnotify in lib dir.

Of course we can set that path on JVM startup, but this approach is not compatible with having complex python apps depending on multiple python libs simultaniously, each one wraps an own java lib.

KOLANICH avatar May 27 '21 13:05 KOLANICH