我知道这个问题已经被问过很多次了,我已经阅读了很多没有正面结果的帖子,因此寻求帮助。
原始来源是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);
}
}
样品1.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
样本1.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
提前感谢您的帮助
更新 -> 答案: Javah -jni Sample1 解决了问题
谢谢