开发者

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 g开发者_Python百科ave 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 (i.e., 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.


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.


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!


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.


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");
    }
}


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 :)


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")


@alee- You can just copy and the paste the dll files in system32 folder of your windows and try to call the library through the System.loadLibrary("mydll")... i guess it may work...


Give the library path in your project as native library location,seems to be solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜