Search code examples
javac++java-native-interface

JNI return random int


I am trying to make a dll for a native java method that returns a random int i have made ones that work before but i cant get this one too m still new to programming with jni and i could use some help this is my c++ source code:

#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);
}

The error is: Exception in thread "main" java.lang.UnsatisfiedLinkError: C:##########: A dynamic link library (DLL) initialization routine failed

Thanks :)

Source for 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));
    }
}

Header file is:

/* 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

Solution

  • Solution!!!! I fixed it by doing some research and through trial and error i figured out that my imports were messing up the DLL

    Cpp file:

    /* 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;
    }
    

    Header file:

    /* 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