loadlibrary - Java: load a library that depends on other libs -


i want load own native libraries in java application. native libraries depend upon third-party libraries (which may or may not present when application installed on client computer).

inside java application, ask user specify location of dependent libs. once have information, using update "ld_library_path" environment variable using jni code. following code snippet using change "ld_library_path" environment variable.

java code

  public static final int setenv(string key, string value) {         if (key == null) {             throw new nullpointerexception("key cannot null");         }         if (value == null) {             throw new nullpointerexception("value cannot null");         }         return nativesetenv(key, value);     }  public static final native int nativesetenv(string key, string value);  

jni code (c)

     jniexport jint jnicall java_test_nativesetenv(jnienv *env, jclass cls, jstring key, jstring value) {     const char *nativekey = null;     const char *nativevalue = null;     nativekey = (*env)->getstringutfchars(env, key, null);     nativevalue = (*env)->getstringutfchars(env, value, null);     int result = setenv(nativekey, nativevalue, 1);     return (jint) result; }  

i have corresponding native methods fetch environment variable.

i can update ld_library_path (this assertion based on output of c routine getenv().

i still not able load native library. dependent third-party libraries still not detected.

any help/pointers appreciated. using linux 64 bit.

edit:

i wrote ssce (in c) test if dynamic loader working. here ssce

 #include  #include  #include  #include   int main(int argc, const char* const argv[]) {      const char* const dependentlibpath = "...:";     const char* const sharedlibrary = "...";     char *newlibpath = null;     char *originallibpath = null;     int l1, l2, result;     void* handle = null;      originallibpath = getenv("ld_library_path");     fprintf(stdout,"\noriginal library path =%s\n",originallibpath);     l1 = strlen(originallibpath);     l2 = strlen(dependentlibpath);     newlibpath = (char *)malloc((l1+l2)*sizeof(char));     strcpy(newlibpath,dependentlibpath);     strcat(newlibpath,originallibpath);     fprintf(stdout,"\nnew library path =%s\n",newlibpath);      result = setenv("ld_library_path", newlibpath, 1);     if(result!=0) {         fprintf(stderr,"\nenvironment not updated\n");         exit(1);     }         newlibpath = getenv("ld_library_path");     fprintf(stdout,"\nnew library path env =%s\n",newlibpath);      handle = dlopen(sharedlibrary, rtld_now);     if(handle==null) {         fprintf(stderr,"\ncould not load shared library: %s\n",dlerror());         exit(1);             }     fprintf(stdout,"\n shared library loaded.\n");      result = dlclose(handle);     if(result!=0) {         fprintf(stderr,"\ncould not unload shared library: %s\n",dlerror());         exit(1);     }      return 0; } 

the c code not work. apparently, dynamic loader not rereading ld_library_path environment variable. need figure out how force dynamic loader re-read ld_library_path environment variable.

see accepted answer here:

changing ld_library_path @ runtime ctypes

in other words, you're trying isn't possible. you'll need launch new process updated ld_library_path (e.g., use processbuilder , update environment() concatenate necessary directory)


Comments