代码之家  ›  专栏  ›  技术社区  ›  Alexzander Bond

JNI返回随机整数

  •  1
  • Alexzander Bond  · 技术社区  · 7 年前

    我正在尝试为一个本机java方法创建一个dll,该方法返回一个随机int。我以前创建过一些可以工作的dll,但我无法获得这个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);
    }
    

    错误是:线程“main”java中出现异常。lang.UnsatifiedLinkError: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
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   user207421    7 年前

    您的CPP文件不包括。h文件,它没有 extern "C" 其自身的声明。因此,这些方法是用C++签名编译的,因此JVM无法找到它们,而JVM需要 外部“C” 根据签名。h文件。

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

        2
  •  -2
  •   Alexzander Bond    7 年前

    解决方案我做了一些研究,通过反复试验,我发现我的导入搞乱了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