Home > OS >  Is there a way to set the Java 'java.library.path' to the program's resources folder?
Is there a way to set the Java 'java.library.path' to the program's resources folder?

Time:06-25

So, I'm making an LWJGL 2 (I need it to be 2, before you say, I'm not gonna upgrade to 3 for this) application.

LWJGL 2 needs the native libraries (DLLs) to run, and I want to know if there is a way to have them in the resources folder (Gradle), as to avoid having the jar in a folder along with the natives.

Any help is appreciated.

CodePudding user response:

No, dlls/so/jnilib files are loaded by the OS. The jvm just calls on the OS to go load it. As one might expect, the OS doesn't know what a jar is, so the JVM cannot tell the OS to load a dll from within it.

It's annoying - but it's just what you have to do: You can stick a DLL in the jar, but then your java code will first have to load it in, save it straight to disk (unpack it), and then load that. There's no need to futz with java.library.path, you can use System.load(absolutePath) to load it once you've unpacked it somewhere.

Where to unpack it to can be a bit of a pickle: Proper OS design dictates that an app should in general not be capable of writing to its own executable. Hence, on e.g. linux the apps tend to be in /bin and a user tends not to have write rights to it, so you can't park it right next to your executable.

Windows, channeling its long and storied history of being a security disaster, often has apps that assume they can write to their own folders in e.g. C:\Program Files\MyAppName. This is bad and you should not write your software to rely on being able to do that.

Writing to a temp folder is also tricky, given that you're about to effectively 'execute' that file, and other users may also be capable of writing to it.

Hence, the best option is to just write to the user's home dir (System.getProperty("user.home") will get you there), then load it.

That, or have an installer that does this for you during the installation phase (where you can rely on having write rights obviously). But now you need an installer which is also quite a drag.

  • Related