JNI - UnsatisfiedLinkError - loadLibrary总是失败 [英] JNI - UnsatisfiedLinkError - loadLibrary always fails

查看:153
本文介绍了JNI - UnsatisfiedLinkError - loadLibrary总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个简单的JNI示例正常工作,但无论我做什么,我都无法使用loadLibrary命令使其工作。如果我指定.so文件的绝对路径并使用System.load而不是System.loadLibrary,它可以正常工作。

I am attempting to get a simple JNI example working, but no matter what I do, I cannot get it to work using the loadLibrary command. It works perfectly if I specify the absolute path of the .so file and use System.load instead of System.loadLibrary.

这是我的目录树:

.
|-- -
|-- TranslatorWrapper.c
|-- TranslatorWrapper.class
|-- TranslatorWrapper.cpp
|-- TranslatorWrapper.h
|-- TranslatorWrapper.java
`-- libTranslatorWrapper.so

这是Java代码:

public class TranslatorWrapper {

    public native String translate(byte[] bytes);

    public static void main(String[] args) {
        TranslatorWrapper w = new TranslatorWrapper();
        System.out.println("From JNI: " + w.translate(null));
    }
    static {
        System.out.println("Attempting to load library from " + System.getProperty("java.library.path"));
        System.loadLibrary("TranslatorWrapper");
        //System.load("/path/to/example/libTranslatorWrapper.so");
    }
}

我知道.so文件需要在java.library.path文件夹,所以我用参数启动程序

I know that the .so file needs to be in the java.library.path folder, so I start the program with the arguments

java TranslatorWrapper -Djava.library.path=.

因为该库与.class文件位于同一目录中。但是,似乎忽略了该值:

since the library is found in the same directory as the .class file. However, it seems that the value is ignored:

Attempting to load library from .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Exception in thread "main" java.lang.UnsatisfiedLinkError: no TranslatorWrapper in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1754)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1045)
    at TranslatorWrapper.<clinit>(TranslatorWrapper.java:14)

注意java.library.path变量我的命令行参数没有改变。

Note that the java.library.path variable has not been changed by my command line argument.

我也知道你用不同的参数调用 loadLibrary 执行加载(特别是删除lib前缀和.so后缀);正如你在代码中看到的那样,我已经在做了。无论.so文件是否在当前目录中,当前目录在java.library.path上,并且我按照我在网上看到的方式调用loadLibrary,这一切都不起作用。

I also know that you call loadLibrary with different arguments that you do load (in particular, removing the lib prefix and the .so suffix); as you can see in the code I'm already doing that. Regardless of the fact that the .so file is in the current directory, that the current directory is on the java.library.path, and that I am calling the loadLibrary in the way I've seen it said online, none of this works.

知道我做错了吗?

推荐答案

我将检查以下内容:


  1. 您确定java进程的当前目录与* .so文件相同吗?有时,包装器脚本可以更改吗?

  2. 是否可以使用 java TranslatorWrapper -Djava.library.path = / path / to / example TranslatorWrapper

  3. 如果您运行的是Mac OS X,请参阅 http://developer.apple.com/java/faq/development.html#anchor4 文件后缀应该是.jnilib(或.dylib)而不是.so

  4. 如果运行Linux,您是否尝试将 / path /添加到/ example / 到LD_LIBRARY_PATH

  1. Are you sure that the current directory of the java process is the same as the *.so file? Sometime, a wrapper script can change that?
  2. Is it working using java TranslatorWrapper -Djava.library.path=/path/to/example TranslatorWrapper
  3. If you are running Mac OS X, then see http://developer.apple.com/java/faq/development.html#anchor4 the file suffix should be .jnilib (or .dylib) and not .so
  4. If running Linux, have you tried appending /path/to/example/ to your LD_LIBRARY_PATH

这篇关于JNI - UnsatisfiedLinkError - loadLibrary总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆