小编Edg*_*ius的帖子

从JNI(Java Native Interface)访问C常量(标题)

如何从Java端(通过JNI)访问C头文件中定义的常量?

我有一个带有头文件c_header.h的C库:

// I'll try to get these values in java
#define PBYTES 64
#define SBYTES 128
#define SGBYTES 128
Run Code Online (Sandbox Code Playgroud)

然后我有这个java代码libbind_jni.java:

package libbind;

public class libbind_jni {
static{
    try {
        System.load("libbind_jni.so");
    } catch (UnsatisfiedLinkError e) {
        System.err.println("Native code library failed to load.\n" + e);
        System.exit(1);
    }
}

public static native String[] pubm(String seed);
Run Code Online (Sandbox Code Playgroud)

然后在libbind_jni.java上运行javah我生成JNI头文件jni_header.h:

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     libbind_libbind_jni
 * Method:    pubm
 * Signature: (Ljava/lang/String;)[Ljava/lang/String;
 */
JNIEXPORT jobjectArray JNICALL Java_lib_f_1jni_pubm
  (JNIEnv *, jclass, jstring);
Run Code Online (Sandbox Code Playgroud)

然后我为JNI库jni_source.c写了一个小C代码: …

c java java-native-interface header constants

5
推荐指数
1
解决办法
2204
查看次数

标签 统计

c ×1

constants ×1

header ×1

java ×1

java-native-interface ×1