.h文件
#include <jni.h>
#include "NativePackage_HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_NativePackage_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) {
printf("Hello world!\n");
return;
}
爪哇
package NativePackage;
public class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("hello");
}
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}
.c文件
#include <jni.h>
#include "NativePackage_HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_NativePackage_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj){
printf("Hello world!\n");
return;
}
我使用
编译了我的.c文件gcc -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/ -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/darwin/ -fPIC -o hello -c HelloWorldImp.c
但是,尝试使用java NativePackage/HelloWorld
运行会产生以下错误:
java -Djava.library.path=NativePackage/ NativePackage/HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at NativePackage.HelloWorld.<clinit>(HelloWorld.java:7)
我正在运行MAC OS X 10.10
答案 0 :(得分:4)
尝试使用
System.load("/home/project/lib/libhello.so");
给出库的绝对路径。
当我遇到同样的问题时帮助了我。答案 1 :(得分:4)
似乎可以使用OS X libhello.dylib 和 -shared 选项的正确命名方案。
gcc -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/ -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/darwin/ -o libhello.dylib -shared HelloWorldImp.c