Java 中的本地方法,Java 中的 C,Java 本地接口 (JNI)
Native method in Java, C in Java, Java Native Interface (JNI)
我创建了本机方法,但我有错误的 签名
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 ) {
}
}
我生成 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
和我的 C. 在 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 ?);
所以,我无法在 galib.c 中添加参数,用什么代替“?”,或者如何创建 .c 文件?
您可以在 C 函数实现中使用任何您喜欢的参数名称。它对任何调用者都没有任何影响,包括 JVM。
话虽如此,对于Java方法参数和C方法参数对应的参数,使用相同的名称是很常见的。其他两个参数表示调用发生的 JNI 环境,以及调用该方法的对象。前者常被命名为env
。我倾向于根据方法所属的 class 来命名后者(例如 algorithm
),但其他名称比比皆是——obj
、o
、self
, 等..
许多可能的组合之一是
JNIEXPORT jintArray JNICALL Java_knapsacproject_algorithm_geneticAlgorithm(
JNIEnv *env, jobject algorithm, jintArray cost, jintArray profit,
jint gens, jint turns, jint cmax) {
// ...
}
另请注意,在您的示例 C 文件中,您提供了一个函数原型,但头文件已经包含一个原型。再提供一个也没有错,但也没用
我创建了本机方法,但我有错误的 签名
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 ) {
}
}
我生成 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
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 ?);
所以,我无法在 galib.c 中添加参数,用什么代替“?”,或者如何创建 .c 文件?
您可以在 C 函数实现中使用任何您喜欢的参数名称。它对任何调用者都没有任何影响,包括 JVM。
话虽如此,对于Java方法参数和C方法参数对应的参数,使用相同的名称是很常见的。其他两个参数表示调用发生的 JNI 环境,以及调用该方法的对象。前者常被命名为env
。我倾向于根据方法所属的 class 来命名后者(例如 algorithm
),但其他名称比比皆是——obj
、o
、self
, 等..
许多可能的组合之一是
JNIEXPORT jintArray JNICALL Java_knapsacproject_algorithm_geneticAlgorithm(
JNIEnv *env, jobject algorithm, jintArray cost, jintArray profit,
jint gens, jint turns, jint cmax) {
// ...
}
另请注意,在您的示例 C 文件中,您提供了一个函数原型,但头文件已经包含一个原型。再提供一个也没有错,但也没用