在 java.library.path 中找不到库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22887779/
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 18:41:19  来源:igfitidea点击:

library not found in java.library.path

javajava-native-interface

提问by sasfour

I'm a newbie to JNI, so I was trying this introduction to JNI tutorial earlier that just calls native to print Hello World! Everything went fine until the point that I wanted to run the java file, at which I keep getting the error: Exception in thread "main": java.lang.UnsatisfiedLinkError: no hello library found in java.library.path. I have googled the error and looked at a lot of peoples' suggestions, but none worked for me unfortunately! I have tried the following:

我是 JNI 的新手,所以我之前尝试了 JNI 教程的介绍,它只是调用 native 来打印 Hello World!一切都很好,直到我想运行 java 文件,在那里我不断收到错误:线程“main”中的异常:java.lang.UnsatisfiedLinkError:在 java.library.path 中找不到 hello 库。我用谷歌搜索了错误并查看了很多人的建议,但不幸的是没有一个对我有用!我尝试了以下方法:

  • Running with command: java -Djava.library.path = "Path to library" HelloWorld
  • setting the LD_LIBRARY_PATH to my .so path
  • 使用命令运行: java -Djava.library.path = " Path to library" HelloWorld
  • 将 LD_LIBRARY_PATH 设置为我的 .so 路径

Everyone else had their issues resolved after doing one of the two above, but not me!

其他人在完成上述两项之一后都解决了他们的问题,但我不是!

Here is the Java Code:

这是Java代码:

public class HelloWorld {

    static {
        System.loadLibrary("hello");
    }

    private native void printHelloWorld();

    public static void main(String[] args) {
        new HelloWorld().printHelloWorld();
    }
} 

And code for native is as follows:

本机代码如下:

void JNICALL Java_printHelloWorld(JNIEnv *env, jobject obj) {
    printf("HelloWorld!");
}

EDIT: I even tried copying the library to the actually directory of java.library.path, but it's still giving me the same error!

编辑:我什至尝试将库复制到 java.library.path 的实际目录,但它仍然给我同样的错误!

回答by Alex Barker

What is your library called? If your paths are correct, your library name is probably wrong. On Windows the file needs to be called hello.dll, OS X (Java < 1.7) libhello.jnilib, OS X (Java >= 1.7) libhello.dyliband just about everything else will be libhello.so. Notice that the Windows dll file is the only file name without the "lib" prefix and that the "lib" prefix is not used when calling System.loadLibrary("hello"). If you are still experiencing a problem loading the lib, try System.load("/path/to/my/libhello.so")to try and load the library directly.

你的图书馆叫什么?如果您的路径正确,则您的库名称可能是错误的。在 Windows 上,文件需要被调用hello.dll,OS X (Java < 1.7) libhello.jnilib,OS X (Java >= 1.7)libhello.dylib和几乎所有其他东西都将是libhello.so. 请注意,Windows dll 文件是唯一没有“lib”前缀的文件名,并且在调用System.loadLibrary("hello"). 如果您仍然遇到加载库的问题,请尝试System.load("/path/to/my/libhello.so")直接加载库。