1. 生成步骤
- 写Java源码(test.Java)
- 编译Java(test.class)
- 把类文件转换成C的头文件(包名+test.h)
1.1 写java文件test.java
package com.mytest;
public class test
{
static {
System.loadLibrary("testJni");
}
public static native int helloworld(String str);
}
1.2 编译java文件
命令行下编译:
javac test.java
1.3 生成jni头文件
使用javah生成jni头文件
javah -classpath E:\code\eclipse-project\test\src com.mytest.test
此处要注意-classpath 是package所在目录,之后跟的是package+类名(不带扩展名)
生成头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_mytest_test */
#ifndef _Included_com_mytest_test
#define _Included_com_mytest_test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_mytest_test
* Method: helloworld
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_mytest_test_helloworld
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif