Loading DLL in Java - Eclipse - JNI
Asked Answered
H

9

13

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?

Halting answered 29/3, 2010 at 3:43 Comment(1)
I tested my classpath with this code System.getProperty("java.class.path") and placed the dll on that specific path. After that, I am getting error "Can't find dependent libraries". How can i check which libraries it is asking :S. I am using some thirdparty Dll.Halting
C
9

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.

Classics answered 29/3, 2010 at 3:48 Comment(0)
D
5

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.

Dael answered 4/4, 2014 at 3:0 Comment(0)
D
4

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!

Dincolo answered 23/7, 2011 at 14:31 Comment(0)
J
3

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.

Junco answered 29/3, 2010 at 6:24 Comment(2)
Looks like, under linux that concept doesn't work somehow, does anyone know why?Roo
It should, worked for me under Linux as well. What kind of library are you trying to load?Junco
E
3

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");
    }
}
Emelda answered 7/8, 2014 at 15:12 Comment(0)
A
2

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

Archon answered 29/7, 2012 at 19:41 Comment(0)
C
1

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

Celestina answered 17/1, 2018 at 7:35 Comment(0)
S
0

@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...

Screeching answered 5/5, 2010 at 4:7 Comment(0)
O
0

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

Orms answered 6/11, 2015 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.