/usr/java/packages/lib/amd64,在ubuntu上使用JNI时出错:java.lang.UnsatisfiedLinkError:no ...在java.library.pat...

I know there are similar questions on this topic but none of the answers could solve my problem:

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

Here is the C++ file:

#include

#include

#include "hjni.h"

JNIEXPORT void JNICALL

Java_hjni_print(JNIEnv *env, jobject obj)

{

printf("Hello World!\n");

return;

}

I compile the C++ file:

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

The files hjni.class, hjni.h, and hjni.so are all created.

I run the java file:

java -Djava.library.path=. hjni

Here is the error I get:

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.(hjni.java:4)

I use java -XshowSettings:properties to check 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

Interestingly, the folder

/usr/java/packages/lib/amd64

does not exist:

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:

/usr/lib/jvm/java-7-openjdk-amd64/lib/amd64/jli

when I add the above path by

export LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/lib/amd64/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.

解决方案

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 .dll for Windows, lib.so for Linux and lib.jnilib for Mac OS X, but I can't find a reference for that right now.

If you rename your file, everything will work.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值