另一个java.lang.UnsatisfiedLinkError与JNI [英] Another java.lang.UnsatisfiedLinkError with JNI

查看:299
本文介绍了另一个java.lang.UnsatisfiedLinkError与JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到一个soultion我的问题,并从昨天寻找。
我使用Windows 7和Eclipse与CDT和MinGW。

I have not found a soultion for my problem and was looking since yesterday. I am using Windows 7 and Eclipse with CDT and 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生成的头文件:

This is my jni javah generated header file:

/* 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文件(x86)\Java\jdk1.7.0_51\bin\javah.exe
工作目录:
$ {workspace_loc:/ ASG / bin}
参数:
-d $ {workspace_loc:/ ASG / asg_jni} $ {java_type_name}

with eclipse external tool thingy: Location: C:\Program Files (x86)\Java\jdk1.7.0_51\bin\javah.exe Working directory: ${workspace_loc:/ASG/bin} Arguments: -d ${workspace_loc:/ASG/asg_jni} ${java_type_name}

这是我的.cpp实现:

This is my .cpp implementation:

/*
 * 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)

任何解决方案?提前感谢。

Any solutions? Thanks in advance.

推荐答案

首先,如果您的本机库在Windows上被称为ASG.dll,将被称为libASG.so * nix系统和libASG.dylib在达尔文。因此,库应该通过其名称加载,允许JVM填充正确的前缀和扩展名。例如: System.loadLibrary(ASG)。请注意,OS X在Java中使用错误的扩展名(.jnilib而不是.dylib) 7。

First, if your native library is called ASG.dll on windows it will be called libASG.so on *nix systems and libASG.dylib on Darwin. Because of this, the library should be loaded by its name, allowing the JVM to populate the correct prefix and extension. Ex: System.loadLibrary("ASG"). Note that OS X uses the wrong extension (.jnilib instead of .dylib) in Java < 7.

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

Now, you still have an UnsatisfiedLinkError, it is because the JVM has no idea where to load that library from. If you use System.loadLibrary, you must set the property java.library.path to the location of your dll file. Alternatively, you may use System.load() to specify a full path and filename (including prefix and extension) of the native library.

这篇关于另一个java.lang.UnsatisfiedLinkError与JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆