0
我嘗試在mac和windows上使用本地方法運行我的java。Windows上的java.lang.UnsatisfiedLinkError JNI錯誤
類與本地方法:
public class NativeMethods {
static {
System.loadLibrary("nativeLib");
}
public static native void printFromJni();
}
類主要方法:
public class Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
NativeMethods.printFromJni();
}
}
JNI頭:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_sdk_NativeMethods */
#ifndef _Included_com_sdk_NativeMethods
#define _Included_com_sdk_NativeMethods
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_sdk_NativeMethods
* Method: printFromJni
* Signature:()V
*/
JNIEXPORT void JNICALL Java_com_sdk_NativeMethods_printFromJni
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
C文件:
#include <stdio.h>
JNIEXPORT void JNICALL Java_com_sdk_NativeMethods_printFromJni
(JNIEnv *env, jclass class){
printf("hello from jni. \n");
}
如果我編譯C文件的MAC:
gcc -o libNativeLib.so -shared -I/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/include nativeLib.c -lc
並運行:
java Demo
它的工作是正確的。
但是,如果我在Windows上編譯的C文件:
gcc -o nativeLib.dll -shared -I"C:\P
rogram Files\Java\jdk1.8.0_60\include" nativeLib.c
-std=c99 -I"C:\Program Files\Java\jdk1.8.0_60\include\win32"
我得到錯誤:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sdk.Nati
veMethods.printFromJni()V
at com.sdk.NativeMethods.printFromJni(Native Method)
at Demo.main(Demo.java:9)
如果我編譯在Mac上的C文件(使用-m32標誌),並複製庫在Windows上我獲得錯誤:
Can't load this .dll (machine code=0x7) on
a IA 32-bit platform
問題在哪裏?請幫忙。
我發現SOLUTION:
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I[Java_HOME]/include -I[Java_HOME]/include/win32 -shared -o Sample1.dll Sample1.c
不要在mac上編譯..在Windows本身編譯。它應該只能以這種方式工作... – spt025