Another java.lang.UnsatisfiedLinkError with JNI



我还没有找到适合我的问题的灵魂,从昨天开始就一直在寻找。我正在使用Windows 7和Eclipse以及CDT和MinGW。

这是我的 JAVA 类:

package pl.asg.front;
public class ASGFrontMain {
static 
{
    System.loadLibrary("libASG");
}
public native void sayHello();
public static void main(String[] args) {
    System.out.println("Hello from JAVA!");
    new ASGFrontMain().sayHello();
}
}

这是我的 jni javah 生成的头文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class pl_asg_front_ASGFrontMain */
#ifndef _Included_pl_asg_front_ASGFrontMain
#define _Included_pl_asg_front_ASGFrontMain
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     pl_asg_front_ASGFrontMain
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_pl_asg_front_ASGFrontMain_sayHello
  (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

使用Eclipse外部工具:位置: C:\Program Files (x86)\Java\jdk1.7.0_51\bin\javah.exe工作目录: ${workspace_loc:/ASG/bin}参数: -d ${workspace_loc:/ASG/asg_jni} ${java_type_name}

这是我.cpp实现:

/*
 * pl_asg_front_ASGFrontMain.c
 *
 *  Created on: 2 kwi 2014
 *      Author: karol
 */
#include "pl_asg_front_ASGFrontMain.h"
JNIEXPORT void JNICALL Java_pl_asg_front_ASGFrontMain_sayHello
  (JNIEnv *env, jobject obj)
{
}

我得到这个输出:

Exception in thread "main" java.lang.UnsatisfiedLinkError: pl.asg.front.ASGFrontMain.sayHello()V
at pl.asg.front.ASGFrontMain.sayHello(Native Method)
Hello from JAVA!
at pl.asg.front.ASGFrontMain.main(ASGFrontMain.java:13)

有什么解决办法吗?提前谢谢。

首先,如果你的原生库被称为 ASG.dll那么在 Windows 上它将被调用 libASG.so 在 *nix 系统上被称为 libASG.dylib,在达尔文上被称为 libASG.dylib。 因此,库应按其名称加载,从而允许 JVM 填充正确的前缀和扩展名。 例如:System.loadLibrary("ASG") . 请注意,OS X 在 Java <7 中使用了错误的扩展名(.jnilib 而不是 .dylib)。

现在,您仍然有一个不满意的LinkError,这是因为JVM不知道从哪里加载该库。 如果使用 System.loadLibrary,则必须将属性 java.library.path 设置为 dll 文件的位置。 或者,您可以使用 System.load() 指定本机库的完整路径和文件名(包括前缀和扩展名)。

最新更新