Search code examples
eclipsedllbuildjna32-bit

JNA finds library running in Eclipse but not when a built JAR file


I am writing a program which needs to use a .dll file. When my program starts up, the following code is executed:

public static void main(String[] args)
{
    String dLLURL = "C:/Program Files (x86)/Location of DLL";
    System.setProperty("jna.library.path", dLLURL);
    System.setProperty("jna.debug_load", "true");
    System.setProperty("jna.debug_load.jna", "true");
    Application.launch(args);
}

I then execute DllInterface dllinterface = (DllInterface) Native.loadLibrary( "dllName.dll", DllInterface.class);

...which loads the correct dll and allows me to use it. JNA outputs the following (this time using the actual name/path of the DLL):

Trying (via loadLibrary) jnidispatch
Looking in classpath from sun.misc.Launcher$AppClassLoader@e2f2a for /com/sun/jna/win32-x86/jnidispatch.dll
Found library resource at jar:file:/C:/Users/bengs_000/Downloads/jna.jar!/com/sun/jna/win32-x86/jnidispatch.dll
Trying C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna2314341730536889248.dll
Found jnidispatch at C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna2314341730536889248.dll
Looking for library 'RailDriver.dll'
Adding paths from jna.library.path: C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Found library 'RailDriver.dll' at C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll

This is what happens when I run the program in Eclipse IDE. It works just fine!

However, when I build/run the program as a .jar, the application says the DLL cannot be found (despite looking in the correct location) and returns the following message:

Trying (via loadLibrary) jnidispatch
Looking in classpath from java.net.URLClassLoader@677327b6 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:rsrc:jna.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Trying C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna8530559464473818762.dll
Found jnidispatch at C:\Users\BENGS_~1\AppData\Local\Temp\jna-792348840\jna8530559464473818762.dll
Looking for library 'RailDriver.dll'
Adding paths from jna.library.path: C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Adding system paths: []
Trying C:\Program Files (x86)\Steam\SteamApps\common\RailWorks\plugins\RailDriver.dll
Looking for lib- prefix
Trying libRailDriver.dll
Looking in classpath from java.net.URLClassLoader@677327b6 for RailDriver.dll
There was an error finding your raildriver.dll file

As you can see, exactly the same path is taken to find the .dll file yet it works in Eclipse and not as a compiled .jar file!

Would you please be able to help me identify the problem? One thing to note is that the .dll file is stored within the 32bit section of Program Files, and my computer is 64bit. I am using Java 32bit to compile and run the program in Eclipse. Surely it should build the .jar with Java 32bit right?


Solution

  • I have found a solution!

    The Background

    I knew that the problem was related to the .dll I wanted to use being 32bit, and my computer being 64bit. In the Eclipse IDE, I was running the app using Java 32bit (I knew I should to this to ensure compatibility).

    When running the .jar file, Java was defaulting to the 64bit version; different to how I was testing it in Eclipse.

    The Solution

    To force the user to use Java 32bit, you need to bundle the .jar into an .exe file. I used Launch4J to do this. There's a parameter in Launch4j which allows you to specify which systems of Java to use. This is located under the JRE tab in the "Search Options" section. By changing it to "32-bit only", the .jar file is launched under Java 32-bit, thus alleviating the problem!

    I hope this might help some one!