JNI方法签名规则
2019-08-01 本文已影响0人
岁月静好忄
本文讲解
不使用javah命令
怎么将.java文件生成相对应的.h文件
首先看看签名表
java类型 | 类型签名 |
---|---|
byte | B |
short | S |
int | I |
long | J |
float | F |
double | D |
char | C |
boolean | Z |
void | V |
数组 | 例如:int[]签名为:[I |
类 | L全限定名;,比如String, 其签名为Ljava/lang/String;(注意后面有个分号) |
_ | _1 |
/ | _ |
; | _2 |
[ | _3 |
例子(主要为同名函数的类型签名)
java代码
public class TestJni
{
/*验证同名函数*/
public native long print(byte b,int i);
public native long print(long l, double d);
/*验证下划线*/
public native void print_m();
public native void print_m(long m);
/*验证"/"和";"*/
public native int print();
public native short print(String s);
}
生成的h头文件(代码中有注释说明)
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestJni */
#ifndef _Included_TestJni
#define _Included_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestJni
* Method: print
* Signature: (BI)J
*/
/*BI分别表示参数byte,int的签名*/
JNIEXPORT jlong JNICALL Java_TestJni_print__BI
(JNIEnv *, jobject, jbyte, jint);
/*
* Class: TestJni
* Method: print
* Signature: (JD)J
*/
/*JD分别表示参数long,double的签名*/
JNIEXPORT jlong JNICALL Java_TestJni_print__JD
(JNIEnv *, jobject, jlong, jdouble);
/*
* Class: TestJni
* Method: print_m
* Signature: ()V
*/
/*_1表示_的签名*/
JNIEXPORT void JNICALL Java_TestJni_print_1m__
(JNIEnv *, jobject);
/*
* Class: TestJni
* Method: print_m
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_TestJni_print_1m__J
(JNIEnv *, jobject, jlong);
/*
* Class: TestJni
* Method: print
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_TestJni_print__
(JNIEnv *, jobject);
/*
* Class: TestJni
* Method: print
* Signature: (Ljava/lang/String;)S
*/
/*Ljava/lang/String;表示String的签名,然后在函数中"/"变成"_",";"变成"_2"*/
JNIEXPORT jshort JNICALL Java_TestJni_print__Ljava_lang_String_2
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif