在 Java 中加载 DLL - Eclipse - JNI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2535700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:00:24  来源:igfitidea点击:

Loading DLL in Java - Eclipse - JNI

javaeclipsejava-native-interface

提问by x.509

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

我正在尝试使用以下代码在 java 中加载 dll 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:/

该项目放在 D:\development\project\ 中,我已将 dll 放在 D: 上。然后我在 eclipse 配置中给出了以下 VM 参数 -Djava.library.path=D:/

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

但是当我运行时,我得到UnsatisifiedLinkerError。在谷歌搜索了一下之后,我使用了 System.load("D:\mydll.dll");

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

但再次遇到同样的问题,有人可以帮忙吗?

回答by Chris Dennett

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

在库路径中指定 DLL 文件名的地方,请忽略它。此外,您的 System.loadLibrary 调用应该只是“mydll”。我可以告诉您(根据经验),如果您将 DLL 放在 Eclipse 中项目的根目录中(即 D:\Eclipse Workspace\Proj),它应该可以工作。任何进一步的链接器错误都可能来自查找其他 DLL 的依赖性问题。例外是一样的。使用 Dependency Walker ( http://www.dependencywalker.com/) 之类的工具来查看您的 DLL 是否依赖于不在系统库路径上的任何其他内容。

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.

编辑:UnsatisfiedLinkError:如果 Java 虚拟机找不到声明为本机的方法的适当本机语言定义,则抛出 - 似乎您正在使用不存在的 JNI 函数。

回答by David Sauter

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

使用System.loadLibrary("mydll")效果很好,你也可以使用那个。如果您使用过javah并且您认为使用 DLL 一切正常,则有两种可能性:

  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 NOTsearched by the JVM, but by Windows itself. And Windows does not know the java.library.path, so it will look in the system PATHvariable for those. If you have the possibility, you can set the system PATHvariable 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.dlldepends on dll_2.dll, which depends on dll_1.dll.

  1. JVM 找不到您的 DLL:在这种情况下,您的 Java 库路径不正确(我对此表示怀疑),您可能应该将其设置为.并将您的 DLL 放在当前工作目录中。
  2. JVM 没有找到您的 DLL 所依赖的 DLL:如果您的 DLL 中有任何依赖库,它们不会被 JVM 搜索,而是被 Windows 本身搜索。Windows 不知道java.library.path,因此它会在系统PATH变量中查找那些。如果有可能,您可以PATH在启动 JVM 之前将系统变量设置为DLL 的位置,一切都会好起来的。或者您可以像这样使用 JVM 加载所有 DLL

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

    哪里dll_3.dll取决于dll_2.dll,这取决于dll_1.dll

Hope that helps.

希望有帮助。

回答by Navin

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

@alee- 您可以将 dll 文件复制并粘贴到 Windows 的 system32 文件夹中,然后尝试通过 System.loadLibrary("mydll") 调用该库...我想它可能会起作用...

回答by mcanti

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!

在此处查看如何正确设置本机依赖项。此外,请确保使用正确的 JVM:在我的情况下,未找到 DLL,因为它是 32 位 DLL,但我使用的是 x64 JVM!

回答by Karoly

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

把你的 Almafa.dll 放到 C:/Java/jre7/lib 或 /bin 对不起,我记不清了。完成不需要更多配置后,只需说

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

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

在类中,您要加载它的位置。它仅适用于 Java 项目,在类似 Android 的项目中您需要使用 JNI。我现在已经发布了 3 天不睡觉的结果:)

回答by user1332994

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

您遇到的一个问题是: System.load("D:\mydll.dll"); 应该是 System.load("D:\\mydll.dll"); 或 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.

我在 System.load 上取得了更大的成功,但 loadlibrary 更适合多平台设计。它会为您计算扩展名。

回答by Yochai Timmer

System.loadLibraryloads the DLL from the JVM path (JDK bin path).

System.loadLibrary从 JVM 路径(JDK bin 路径)加载 DLL。

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

如果要加载带有路径的显式文件,请使用System.load()

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

另请参阅:Java 中 System.load() 和 System.loadLibrary 之间的区别

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

回答by Ibin Joy

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

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

回答by Mansi

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

而不是使用 System.load("myDLL.dll")