运行一个非常简单的Hello World JNI测试时遇到麻烦

Trouble running a very simple Hello World JNI test

本文关键字:JNI World Hello 测试 麻烦 遇到 简单 非常 一个 运行      更新时间:2023-10-16

EDIT3:这是公然的编译器标志导致的问题,如果我从命令行编译使用微软的编译器它工作正常。有人知道我需要改变代码::块来解决这个问题吗?

当我运行我的代码,我得到一个UnsatisfiedLinkError。加载步骤工作正常,当它实际调用代码时,我得到错误。

从我的dll路径开始

java -Djava.library.path=E:JavaJNIHellowWorldPasswordGenHWbinDebug -jar distJNIHellowWorld.jar

异常

Exception in thread "main" java.lang.UnsatisfiedLinkError: jnihellowworld.Main.HelloWorld()Ljava/lang/String;
        at jnihellowworld.Main.HelloWorld(Native Method)
        at jnihellowworld.Main.main(Main.java:16)

Java代码
package jnihellowworld;
import java.io.IOException;
public class Main {
    public native String HelloWorld() throws Error;
    public static void main(String[] args) throws IOException {
        try
        {
        System.loadLibrary("PasswordGenHW");
        String test = new Main().HelloWorld(); //errors on this line
        System.out.println(test);
        System.in.read();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
c++头

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jnihellowworld_Main */
#ifndef _Included_jnihellowworld_Main
#define _Included_jnihellowworld_Main
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     jnihellowworld_Main
 * Method:    HelloWorld
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_jnihellowworld_Main_HelloWorld
  (JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
c++ cpp

#include "jnihellowworld_Main.h"
#include <jni.h>
JNIEXPORT jstring JNICALL Java_jnihellowworld_Main_HelloWorld
  (JNIEnv *env, jobject obj)
  {
      return env->NewStringUTF("Hello world!");
  }

我正在使用Code::Blocks和GCC来完成dll。

编辑:这里是dll的dumpbin/exports

2    1 000011D8 Java_jnihellowworld_Main_HelloWorld@8

EDIT2:使用system.loadLibrary()简化项目以复制错误

尝试在编译和链接上添加这些标志:

-Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at

的例子:

gcc -c -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at test.c
gcc -shared -D_JNI_IMPLEMENTATION_ -Wl,--kill-at test.o -o mylib.dll

最后我只能用微软的编译器手工编译