简体   繁体   English

在 java.library.path 中找不到库

[英]library not found in java.library.path

I'm a newbie to JNI, so I was trying this introduction to JNI tutorial earlier that just calls native to print Hello World!我是 JNI 的新手,所以我之前尝试了 JNI 教程的介绍,它只是调用 native 来打印 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.一切都很好,直到我想运行 java 文件,在那里我不断收到错误:线程“main”中的异常:java.lang.UnsatisfiedLinkError:在 java.library.path 中找不到 hello 库。 I have googled the error and looked at a lot of peoples' suggestions, but none worked for me unfortunately!我用谷歌搜索了错误并查看了很多人的建议,但不幸的是没有一个对我有用! I have tried the following:我尝试了以下方法:

  • Running with command: java -Djava.library.path = " Path to library " HelloWorld使用命令运行: java -Djava.library.path = " Path to library " HelloWorld
  • setting the LD_LIBRARY_PATH to my .so path将 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 的实际目录,但它仍然给我同样的错误!

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.dylib and just about everything else will be libhello.so .在 Windows 上,该文件需要被称为hello.dll ,OS X (Java < 1.7) libhello.jnilib ,OS X (Java >= 1.7) libhello.dylib和几乎所有其他东西都是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") .请注意,Windows dll 文件是唯一没有“lib”前缀的文件名,并且在调用System.loadLibrary("hello")时不使用“lib”前缀。 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.如果您仍然遇到加载库的问题,请尝试System.load("/path/to/my/libhello.so")尝试直接加载库。

@Alex Barker The version @Alex Barker 版本

System.load("/path/to/my/libhello.so")

does not resolve dependencies.不解决依赖关系。 If there are dependencies upon other user-defined libraries, they need to be loaded before.如果对其他自定义库有依赖,则需要先加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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