简体   繁体   中英

Loading DLL in Java - Eclipse - JNI

I am trying to load a dll in java using the following code System.loadLibrary("mydll");

The project is placed in D:\\development\\project\\ and i have placed the dll on D:. I then gave following VM argument in eclipse configuration -Djava.library.path=D:/

But when i run i get UnsatisifiedLinkerError . After googling a bit, I used System.load("D:\\mydll.dll");

but again getting the same problem, could someone can help?

Where you specify the DLL filename in the library path, omit that. Additionally, your System.loadLibrary call should just be 'mydll'. I can tell you (from experience) that if you put the DLL in the root of your project in Eclipse (ie, D:\\Eclipse Workspace\\Proj), it should work. Any further linker errors could be from dependency problems with finding other DLLs. The exception is the same. Use something like Dependency Walker ( http://www.dependencywalker.com/ ) to see if your DLL relies on anything else not on the system library path.

Edit: UnsatisfiedLinkError : Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native -- it seems like you are using a JNI function which does not exist.

Check out how to properly set up the native dependencies here . Additionally, make sure you use the correct JVM: in my case, the DLL was not found because it was a 32 bit DLL, but I used the x64 JVM!

One problem you have is: System.load("D:\\mydll.dll"); should be System.load("D:\\\\mydll.dll"); or System.load("D:/mydll.dll");

I have had more success with System.load, but loadlibrary is better designed for multiplatform. It figures out the extension for you.

Using System.loadLibrary("mydll") works fine, you can also use that one. If you used javah and you think with your DLL everything is fine, there are two possibilies:

  1. The JVM does not find your DLL : In this case, your java library path is not correct (which I doubt) and you should probably set it to . and place your DLL in the current working dir.
  2. The JVM does not find a DLL your DLL depends on : If you have any dependent libraries in your DLL, they are NOT searched by the JVM, but by Windows itself. And Windows does not know the java.library.path , so it will look in the system PATH variable for those. If you have the possibility, you can set the system PATH variable to the location of your DLLs before starting the JVM and everything will be fine. Or you can load all your DLLs using the JVM like this

    System.loadLibrary("dll_1");
    System.loadLibrary("dll_2");
    System.loadLibrary("dll_3");

    where dll_3.dll depends on dll_2.dll , which depends on dll_1.dll .

Hope that helps.

Put your Almafa.dll into the C:/Java/jre7/lib or /bin sorry, I can`t remember exactly. After you have done no more configuration needed, just say

static{ System.LoadLibrary("Almafa"); }

in the class, where you want to load it. It is works only in Java project, in Android like project you need to use JNI. I had posted now the result of 3 days no sleeping :)

System.loadLibrary loads the DLL from the JVM path (JDK bin path).

If you want to load an explicit file with a path, use System.load ()

See also: Difference between System.load() and System.loadLibrary in Java

public class MyClass
{
    static 
    {
        System.load("MyJNI.dll");
    }
}

@ alee-你可以复制并粘贴windows的system32文件夹中的dll文件,并尝试通过System.loadLibrary(“mydll”)调用库...我猜它可能有用......

将项目中的库路径作为本机库位置,似乎已解决。

I got my error resolved by using the following:

   static {
    try {
        System.loadLibrary("myDLL");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Instead of using System.load("myDLL.dll")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM