简体   繁体   中英

Native method in Java, C in Java, Java Native Interface (JNI)

I create native method but I have bad signature

  • algorithm.java

 package knapsacproject; public class algorithm { /** * * @param cost * @param profit * @param cmax * @param gens * @param turns * @return */ public native int [] geneticAlgorithm(int[] cost, int[] profit, int gens, int turns,int cmax); static { try { System.load("C:/Users/Desktop/dp/KnapSacProject/src/knapsacproject/helllo.dll"); System.out.println("loaded successfully"); } catch (Exception e){ e.printStackTrace(); } } protected int[] cost, profit, result; protected int gens, turns, cmax; public algorithm(int[] cost,int[] profit, int gens ,int turns , int cmax ) { this.cost=cost; this.profit=profit; this.gens=gens; this.turns=turns; this.cmax=cmax; } public int[] getResult(){ return geneticAlgorithm(cost, profit, gens, turns, cmax); } public static void main (String[] args ) { } } 

my generate header

knapsacproject_algorithm.h

 /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class knapsacproject_algorithm */ #ifndef _Included_knapsacproject_algorithm #define _Included_knapsacproject_algorithm #ifdef __cplusplus extern "C" { #endif /* * Class: knapsacproject_algorithm * Method: geneticAlgorithm * Signature: ([I[IIII)[I */ JNIEXPORT jintArray JNICALL Java_knapsacproject_algorithm_geneticAlgorithm ( JNIEnv *, jobject , jintArray , jintArray , jint , jint , jint); #ifdef __cplusplus } #endif #endif 
and my C. in Java

galib.c

 #include <jni.h> #include "knapsacproject_algorithm.h" #include "gaParameters.h" #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> JNIEXPORT jintArray JNICALL Java_knapsacproject_algorithm_geneticAlgorithm ( JNIEnv * ?, jobject ?, jintArray ?, jintArray ?, jint ? , jint ?, jint ?); 

So, I can not add parameters in my galib.c and what will be instead of "?", or how to create .c file?

You can use whatever parameter names you like in the C function implementation. It makes no difference whatsoever to any caller, including the JVM.

With that said, for the parameters that correspond between the Java method parameters and the C method parameters, it is common to use the same names. The other two parameters represent the JNI environment in which the call occurs, and the object on which the method is invoked. The former is often named env . I tend to name the latter according to the class to which the method belongs (eg algorithm ), but other names abound -- obj , o , self , etc ..

One of the many possible combinations would be

JNIEXPORT jintArray JNICALL Java_knapsacproject_algorithm_geneticAlgorithm(
        JNIEnv *env, jobject algorithm, jintArray cost, jintArray profit,
        jint gens, jint turns, jint cmax) {
    // ...
}

Note, too, that in your example C file you present a function prototype, but the header file already contains a prototype. It is not wrong to provide another, but neither is it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM