简体   繁体   English

另一个带有JNI的java.lang.UnsatisfiedLinkError

[英]Another java.lang.UnsatisfiedLinkError with JNI

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. 我正在将Windows 7和Eclipse与CDT和MinGW一起使用。

This is my JAVA class: 这是我的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();
}
}

This is my jni javah generated header file: 这是我的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

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} 使用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}

This is my .cpp implementation: 这是我的.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)
{

}

And i am getting this output: 我得到这个输出:

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. 提前致谢。

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. 首先,如果您的本机库在Windows上称为ASG.dll,则在* nix系统上将称为libASG.so,在达尔文上将称为libASG.dylib。 Because of this, the library should be loaded by its name, allowing the JVM to populate the correct prefix and extension. 因此,应该按名称加载库,从而允许JVM填充正确的前缀和扩展名。 Ex: System.loadLibrary("ASG") . 例如: System.loadLibrary("ASG") Note that OS X uses the wrong extension (.jnilib instead of .dylib) in Java < 7. 请注意,在Java <7中,OS X使用错误的扩展名(.jnilib而不是.dylib)。

Now, you still have an UnsatisfiedLinkError, it is because the JVM has no idea where to load that library from. 现在,您仍然有一个UnsatisfiedLinkError,这是因为JVM不知道从何处加载该库。 If you use System.loadLibrary, you must set the property java.library.path to the location of your dll file. 如果使用System.loadLibrary,则必须将属性java.library.path设置为dll文件的位置。 Alternatively, you may use System.load() to specify a full path and filename (including prefix and extension) of the native library. 或者,您可以使用System.load()指定本机库的完整路径和文件名(包括前缀和扩展名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM