1

Maybe another "it can't be done" question :)

I have a Java application which can be built either 32 bit or 64 bit. I want to build and test both versions on the same machine.

I have a native library that needs to be loaded (really set of DLLs) that that are built and output in two directories.

.../x64/.dll .../x32/.dll

The "..." part is set in the java "java.library.path"

In the app startup the code does:

    try {
        System.loadLibrary("x32/native-library");
        libraryLoaded_ = true;
    } catch(Throwable e) {
        try {
            System.loadLibrary("x64/native-library");
            libraryLoaded_ = true;
        } catch(Throwable t) {
            log.debug("failed to load module " + e); // : \"" + path + "\".",e);
            log.debug("    or win8 module " + t); // : \"" + path + "\".",e);
        }
    }

This works (in part) because loading the 32 bit library will fail when the program is compiled java 64 bit and it will then try the 64 bit version.

However native-library.dll is dependent on other DLLS which are in the same folder as itself but they will not resolve unless that directory in the system path environment variable of the java process.

However if both the x32 and x64 directories are in the system path and the dlls in both have the same names there is ambiguity in terms of which dependent DLLs will load.

I want to have the native-library.dll to load it's dependencies first from the folder it is resident in.

How might I accomplish this? The java.library.path seems to have no effect on the loading of dependent dlls of the library being explicitly loaded.

I figure I need to somehow modify the path in the java app's process to include the native-library's location before calling "loadLibrary".

Is it possible to do this in the app code? Is there any way to make a dll loaded by loadLibrary load it dependencies from it's folder first before searching the process's path elements ?

peterk
  • 5,136
  • 6
  • 33
  • 47

1 Answers1

0
  • In general, you can't alter "java.library.path" after JVM start (there are some dirty tricks to do that using reflection but it's better not).
  • You can use String archDataModel = System.getProperty("sun.arch.data.model"); to determine if your JVM is 32bit or 64bit.
  • You can use a custom ClassLoader with modified findLibrary method to load library from a correct place. However this let's you specify path only for the loaded library itself but not for it's dependencies. You can change dependencies only by changing PATH environment variable or by changing current directory.
Alexei Osipov
  • 728
  • 10
  • 17