[英]JNI UnsatisfiedLinkError without wrong method names and with library path specified
I'm trying to work build my very first JNI application, following this tutorial: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html 我正在尝试构建我的第一个JNI应用程序,遵循本教程: https : //www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
Problem Summary: While running my application, I get an java.lang.UnsatisfiedLinkError error. 问题摘要:运行我的应用程序时,出现java.lang.UnsatisfiedLinkError错误。
First I wrote the Class HelloJNI.java : 首先我写了HelloJNI.java类:
public class HelloJNI {
static {
System.loadLibrary("hello"); // Load native library at runtime
// hello.dll (Windows) or libhello.so (Unixes)
}
// Declare a native method sayHello() that receives nothing and returns void
private native void sayHello();
// Test Driver
public static void main(String[] args) {
new HelloJNI().sayHello(); // invoke the native method
}
}
This class I compiled with: javac HelloJNI.java Next I ran javah HelloJNI This produced the following file HelloJNI.h : 我编译的这个类: javac HelloJNI.java接下来我运行javah HelloJNI这产生了以下文件HelloJNI.h :
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJNI */
#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloJNI_sayHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Next I implemented HelloJNI.c : 接下来我实现了HelloJNI.c :
#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"
// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
Finally I compiled the c class: 最后我编译了c类:
This produces the files hello.so and HelloJNI.o. 这将生成文件hello.so和HelloJNI.o。 Next I try to run the code:
接下来我尝试运行代码:
java -Djava.library.path=. java -Djava.library.path =。 HelloJNI This produces the error:
HelloJNI这会产生错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at HelloJNI.(HelloJNI.java:3) 线程“main”中的异常java.lang.UnsatisfiedLinkError:java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)的java.library.path中没有hello,位于java.lang.Runtime.loadLibrary0(Runtime.java:870)在HelloJNI的java.lang.System.loadLibrary(System.java:1122)。(HelloJNI.java:3)
This seems to be the most common JNI error on the internet... My method names seem to be correct. 这似乎是互联网上最常见的JNI错误...我的方法名称似乎是正确的。 I also ran:
我也跑了:
This gives me: 00000000000006b0 T Java_HelloJNI_sayHello which seems to be correct, ie the compiler didn't add additional characters. 这给了我: 00000000000006b0 T Java_HelloJNI_sayHello似乎是正确的,即编译器没有添加额外的字符。 I simply ran out of ideas of things I could try.
我简单地用尽了我可以尝试的事情。 Any suggestions?
有什么建议?
My OS: Linux Mint 13, GCC version 4.7.3, java version 1.8.0_60 我的操作系统:Linux Mint 13,GCC 4.7.3版,java版1.8.0_60
==========UPDATE=============== When I replace System.loadLibrary("hello");
========== UPDATE ===============当我替换
System.loadLibrary("hello");
by System.load("/usr0/home/jkinable/workspace/javaIDEA/jnitest/hello.so");
通过
System.load("/usr0/home/jkinable/workspace/javaIDEA/jnitest/hello.so");
my HelloWorld example works! 我的HelloWorld示例有效! However, I don't want to use an absolute path so I'm still looking for a way to use
System.loadLibrary("hello");
但是,我不想使用绝对路径,所以我仍然在寻找使用
System.loadLibrary("hello");
instead? 代替? Any suggestions?
有什么建议? I've also tried running on a different linux system, but I get the same issue.
我也试过在不同的Linux系统上运行,但我遇到了同样的问题。
It turns out that the problem is due to some naming convention on unix/linux platforms! 事实证明,问题是由于unix / linux平台上的一些命名约定! When using: System.loadLibrary("hello");
使用时:System.loadLibrary(“hello”); the file should not be named hello.so!
该文件不应该命名为hello.so! Instead, the name should be libhello.so .
相反,名称应该是libhello.so 。 On Windows, use hello.dll.
在Windows上,使用hello.dll。 I'm surprised that this issue is not mentioned in IBM's JNI tutorial: http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html
我很惊讶IBM的JNI教程中没有提到这个问题: http : //www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html
I'm not sure what the rationality behind this issue is. 我不确定这个问题背后的合理性是什么。 Why would you load a library "hello" which should be named libhello.so on your filesystem (instead of hello.so)?
为什么要在文件系统(而不是hello.so)上加载一个名为libhello.so的库“hello”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.