我理解这个问题已被多次询问,我已阅读了很多帖子而没有积极的结果,因此寻求帮助。
原始来源为Java Programming with JNI
我收到以下错误
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libSample1 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Sample1.main(Sample1.java:10)
源代码如下
Sample1.java
public class Sample1
{
public native int intMethod(int n);
public static void main(String[] args)
{
System.loadLibrary("libSample1");
Sample1 sample = new Sample1();
int square = sample.intMethod(5);
System.out.println("intMethod " + square);
}
}
Sample1.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Sample1 */
#ifndef _Included_Sample1
#define _Included_Sample1
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_Sample1_intMethod
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
Sample1.cpp
#include "Sample1.h"
#include <string.h>
JNIEXPORT jint JNICALL Java_Sample1_intMethod(JNIEnv *env, jobject obj, jint num)
{
return num * num ;
}
操作系统:Ubuntu 12.04 LTS
编译命令
g++ -shared -fPIC -I/usr/lib/jvm/jdk1.6.0_45/include -I/usr/lib/jvm/jdk1.6.0_45/include/linux Sample1.cpp -o libSample1.so
我也试过
java -Djava.library.path=/home/userid/java_proj/example Sample1
感谢您提前获得帮助
更新 - &gt;答案: Javah -jni Sample1修复了问题
由于