Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回随机整数 我试图为一个原生java方法做一个DLL,它返回一个随机int,我以前做过一些,但是我不能用这个新的M来编程,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); }_Java_C++_Java Native Interface - Fatal编程技术网

返回随机整数 我试图为一个原生java方法做一个DLL,它返回一个随机int,我以前做过一些,但是我不能用这个新的M来编程,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); }

返回随机整数 我试图为一个原生java方法做一个DLL,它返回一个随机int,我以前做过一些,但是我不能用这个新的M来编程,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); },java,c++,java-native-interface,Java,C++,Java Native Interface,头文件是: /* 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

头文件是:

/* 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
/*不要编辑此文件-它是机器生成的*/
#包括
/*类NativeRandom的标头*/
#如果不包括国家和地区
#定义包括在内的国家和地区
#ifdef_uucplusplus
外部“C”{
#恩迪夫
/*
*类别:NativeRandom
*方法:下一步
*签字:(一)一
*/
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u I
(JNIEnv*,jclass,jint);
/*
*类别:NativeRandom
*方法:下一步
*签字:(二)一
*/
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u II
(JNIEnv*,jclass,jint,jint);
#ifdef_uucplusplus
}
#恩迪夫
#恩迪夫

解决方案!!!!我做了一些研究,通过反复试验,我发现我的导入搞乱了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;
}
/*将“dll.h”替换为标头的名称*/
#包括“忽略.h”
#包括
#包括
#包括
#包括
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u I
(JNIEnv*env,jclass clazz,jint i){
srand(时间(空));
int n=(rand()%i)+1;
返回n;
}
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u II
(JNIEnv*env,jclass clazz,jint seed,jint i){
srand(种子);
int n=(rand()%i)+1;
返回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
/*不要编辑此文件-它是机器生成的*/
#包括
/*类NativeRandom的标头*/
#如果不包括国家和地区
#定义包括在内的国家和地区
#ifdef_uucplusplus
外部“C”{
#恩迪夫
/*
*类别:NativeRandom
*方法:下一步
*签字:(一)一
*/
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u I
(JNIEnv*,jclass,jint);
/*
*类别:NativeRandom
*方法:下一步
*签字:(二)一
*/
JNIEXPORT jint JNICALL Java\u NativeRandom\u next\u II
(JNIEnv*,jclass,jint,jint);
#ifdef_uucplusplus
}
#恩迪夫
#恩迪夫

您的CPP文件不包括.h文件,并且它没有自己的
外部“C”
声明。所以,这些方法是用C++签名编译的,所以JVM不能找到,它期望按.h文件的代码>外“C”< /代码>签名。
最简单的解决方法是包含.h文件。

但是我也不能得到这个文件-什么不起作用?为什么?预期产量是多少?你得到了什么?如何调用本机方法?有什么错误吗?如果是,堆栈跟踪是什么?您已经发布了要求和一些C++,但是您仍然需要更多地讲述:这个代码是如何工作的?怎么了?你到底被困在哪里?请仔细阅读并研究和,以查看发布到此站点的问题的预期结果。我添加了错误,这是javapublic类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));}好的,我添加了源java代码。不要使用“答案”部分为您的帖子添加更多信息。@EJoshuaS这是一个解决方案,我只是忘了添加为什么我有点匆忙,是导入弄乱了它,但我现在修复了它。我将文件命名为IGNORE.cpp和IGNORE.h,因为我不想命名它们
/* 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