我已经在stackoverflow上查找了类似的问题,但我找到的解决方案似乎都没有为我工作。我在Linux / Ubuntu机器上。我刚刚练习JNI,但是我收到了这个错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: nativetest.sayHello(Ljava/lang/String;)Ljava/lang/String;
at nativetest.sayHello(Native Method)
at nativetest.main(nativetest.java:8)
我提供了我的.c,.h和.java文件。
.java文件:
public class nativetest
{
System.loadLibrary("nativetest");
public native String sayHello(String s);
public static void main(String[] argv)
{
String retval = null;
nativetest nt = new nativetest();
retval = nt.sayHello("Hi");
System.out.println("Invocation returned " + retval);
}
}
.c文件:
#include "nativetest.h"
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
JNIEXPORT jstring JNICALL Java_nativetest_sayHello(JNIEnv *env, jobject thisobject, jstring js)
{
return js;
}
.h文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class nativetest */
#ifndef _Included_nativetest
#define _Included_nativetest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: nativetest
* Method: sayHello
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_nativetest_sayHello
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
我使用这些命令生成.h文件,编译/生成.so文件,然后运行:
javac nativetest.java
javah -jni nativetest
gcc --shared -o libnativetest.so -I / usr / lib / jvm / java-7-openjdk-amd64 / include -I / usr / lib / jvm / java-7-openjdk-amd64 / include / linux nativetest.c
java -Djava.library.path =。 nativetest
我目前在libnativetest.so文件和所有.c,.java和.h文件的目录中。
任何帮助都将不胜感激。
答案 0 :(得分:0)
好的,结果是对
的号召System.loadLibrary("nativetest");
应该是静态的:
static{
System.loadLibrary("nativetest");
}
我在使用javac进行重新编译时也犯了错误。我是Linux新手:)