gpt4 book ai didi

java - JNI 返回随机整数

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:15 31 4
gpt4 key购买 nike

我正在尝试为返回随机 int 的 native java 方法制作一个 dll 我以前做过一些可以工作的但是我也不能得到这个我对使用 jni 编程还是新手我可以使用一些帮助这是我的C++ 源代码:

#include "IGNORE.h"
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <iostream>

JNIEXPORT jint JNICALL Java_NativeRandom_next__I
(JNIEnv *env, jclass clazz, jint i){
srand(time(0));
return (jint) (rand()%i)
}

JNIEXPORT jint JNICALL Java_NativeRandom_next__II
(JNIEnv *env, jclass clazz, jint seed, jint i){
srand((int)seed);
return (jint) (rand()%i);
}

错误是:Exception in thread "main"java.lang.UnsatisfiedLinkError: C:##########: A dynamic link library (DLL) initialization routine failed

谢谢:)

JAVA 源代码:

public class NativeRandom {
public static native int next(int h);
public static native int next(int h, int seed);

public static void main(String[] args) {
System.load("C:\\dlls\\RP.dll");
System.out.println(next(4));
}
}

头文件是:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class NativeRandom */

#ifndef _Included_NativeRandom
#define _Included_NativeRandom
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: NativeRandom
* Method: next
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_NativeRandom_next__I
(JNIEnv *, jclass, jint);

/*
* Class: NativeRandom
* Method: next
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_NativeRandom_next__II
(JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

最佳答案

您的 CPP 文件不包含 .h 文件,并且它没有自己的 extern "C" 声明。因此,这些方法是使用 C++ 签名编译的,因此 JVM 无法找到它们,而 JVM 需要根据 .h 文件的 extern "C" 签名。

简单的解决方法是包含 .h 文件。

关于java - JNI 返回随机整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577043/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com