在ubuntu上使用JNI时出错:java.lang.UnsatisfiedLinkError:no ...在java.library.path中 [英] Error when using JNI on ubuntu: java.lang.UnsatisfiedLinkError: no ... in java.library.path
问题描述
我知道这个主题有类似的问题,但没有答案可以解决我的问题:
I know there are similar questions on this topic but none of the answers could solve my problem:
我有一个java文件:
I have a java file:
class hjni {
static {
System.loadLibrary("hjni");
}
private native void print();
public static void main(String[] args) {
new hjni().print();
}
}
我使用以下命令来编译和生成头文件:
I use the following to compile and generate header files:
javac hjni.java
javah -jni hjni
这里是C ++文件:
#include <jni.h>
#include <stdio.h>
#include "hjni.h"
JNIEXPORT void JNICALL
Java_hjni_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}
我编译C ++文件:
g++ -fPIC -shared -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux hjni.cpp -o hjni.so
文件 hjni.class
, hjni.h
和 hjni.so
我运行java文件:
java -Djava.library.path=. hjni
这是我得到的错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hjni in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1889)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at hjni.<clinit>(hjni.java:4)
我使用 java -XshowSettings:properties
检查
java.library.path
:
java.library.path = /usr/java/packages/lib/amd64
/usr/lib/x86_64-linux-gnu/jni
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
/usr/lib/jni
/lib
/usr/lib
有趣的是,文件夹
/usr/java/packages/lib/amd64
不存在:
cd /usr/java/packages/lib/amd64
bash: cd: /usr/java/packages/lib/amd64: No such file or directory
I could find a shared library (libjli.so) here:
当我通过
when I add the above path by
export LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/lib/amd64/jli
路径已成功添加,打开。如果我关闭终端并重新打开它,则添加的路径不再存在。我还将 hjni.so
复制到 jli
文件夹,但我得到了相同的链接错误。
the path is successfully added as long as the terminal is open. If I close the terminal and reopen it, then the added path is no longer there. I also copied hjni.so
to the jli
folder but I got the same linking error.
推荐答案
当你执行 System.loadLibrary(hjni)
libhjni.so
。调用 System.mapLibraryName(libname)
,找出给定lib的文件名应该是什么。不幸的是, mapLibraryName
的Javadoc仅声明该名称将被映射到一个平台相关名称,但不是构建此文件名的规则。它是< name> .dll
对于Windows, lib< name> .so
对于Linux和 lib< name> .jnilib
对于Mac OS X,但是我现在找不到它的引用。
When you do System.loadLibrary("hjni")
Java will look for a file called libhjni.so
. Call System.mapLibraryName(libname)
to find out what the file name for a given lib should be. Unfortunately the Javadoc for mapLibraryName
only states the name will be mapped to a platform dependent name but not what the rules for building this file name are. It's <name>.dll
for Windows, lib<name>.so
for Linux and lib<name>.jnilib
for Mac OS X, but I can't find a reference for that right now.
如果您重命名文件,一切都会奏效。
If you rename your file, everything will work.
这篇关于在ubuntu上使用JNI时出错:java.lang.UnsatisfiedLinkError:no ...在java.library.path中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!