我正在尝试为一个本机java方法创建一个dll,它返回一个随机的int我已经制作了之前有效的但是我不能得到这个也是新的编程用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);
}
错误是:线程“main”中的异常java.lang.UnsatisfiedLinkError:C:##########:动态链接库(DLL)初始化例程失败
谢谢:)
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
答案 0 :(得分:0)
您的CPP文件不包含.h文件,并且它没有自己的extern "C"
声明。因此,这些方法是使用C ++签名编译的,因此JVM无法找到它们,JVM根据.h文件需要extern "C"
个签名。
简单的解决方法是包含.h文件。
答案 1 :(得分:-2)
解!!!!我通过做一些研究并通过反复试验来修复它,我发现我的导入搞乱了DLL
Cpp文件:
/* Replace "dll.h" with the name of your header */
#include "IGNORE.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
JNIEXPORT jint JNICALL Java_NativeRandom_next__I
(JNIEnv *env, jclass clazz, jint i){
srand(time(NULL));
int n = (rand()%i)+1;
return n;
}
JNIEXPORT jint JNICALL Java_NativeRandom_next__II
(JNIEnv *env, jclass clazz, jint seed, jint i){
srand(seed);
int n =(rand()%i)+1;
return n;
}
标题文件:
/* 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