【问题标题】:Another java.lang.UnsatisfiedLinkError with JNIJNI 的另一个 java.lang.UnsatisfiedLinkError
【发布时间】:2014-05-14 06:29:18
【问题描述】:

我还没有找到解决我的问题的方法,从昨天开始就一直在寻找。 我正在使用带有 CDT 和 MinGW 的 Windows 7 和 Eclipse。

这是我的 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)

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

【问题讨论】:

  • 如何构建libASG?它需要是 DLL,而不是 .lib。

标签: java c++ eclipse java-native-interface


【解决方案1】:

首先,如果您的本地库在 Windows 上称为 ASG.dll,在 *nix 系统上将称为 libASG.so,在 Darwin 上将称为 libASG.dylib。因此,库应该按其名称加载,允许 JVM 填充正确的前缀和扩展名。例如:System.loadLibrary("ASG")。 请注意,OS X 在 Java

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多