1. 编写Java类
package hellojni;
/*
* @(#)HelloWorld.java
*
* Copyright 2007 School of Software, Yunnan University.
* All rights reserved
*/
/**
* 载入C++写的业务实现库,Java端的调用接口
* @author 88250
* @version 1.0.0.0, Jan 31, 2008
*/
public class HelloWorld {
static {
try {
// 此处即为本地方法所在链接库名
System.load("/home/daniel/Work/Sources/Java/HelloJNI/build/classes/libHelloWorld.so");
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
}
/**
* 业务逻辑方法
* @param strName
*/
public native void SayHello(String strName);
}
/*
* @(#)HelloWorld.java
*
* Copyright 2007 School of Software, Yunnan University.
* All rights reserved
*/
/**
* 载入C++写的业务实现库,Java端的调用接口
* @author 88250
* @version 1.0.0.0, Jan 31, 2008
*/
public class HelloWorld {
static {
try {
// 此处即为本地方法所在链接库名
System.load("/home/daniel/Work/Sources/Java/HelloJNI/build/classes/libHelloWorld.so");
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
}
/**
* 业务逻辑方法
* @param strName
*/
public native void SayHello(String strName);
}
2. 编译java文件生成HelloWorld.class文件
3. 在命令行下,javah -jni hellojni.HelloWorld 生成 HelloWorld.h 文件。(注意:运行javah命令的路径为hellojni包的上层)
生成hellojni_HelloWorld.h文件,如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class hellojni_HelloWorld */
#ifndef _Included_hellojni_HelloWorld
#define _Included_hellojni_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: hellojni_HelloWorld
* Method: SayHello
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_hellojni_HelloWorld_SayHello
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
#include <jni.h>
/* Header for class hellojni_HelloWorld */
#ifndef _Included_hellojni_HelloWorld
#define _Included_hellojni_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: hellojni_HelloWorld
* Method: SayHello
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_hellojni_HelloWorld_SayHello
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
4. 编写HelloWorld.cpp
#include "hellojni_HelloWorld.h"
#include <iostream>
// 与hellojni_HelloWorld.h中函数声明相同
JNIEXPORT void JNICALL Java_hellojni_HelloWorld_SayHello(JNIEnv * env, jobject arg,
jstring instring)
{
// 从instring字符串取得指向字符串UTF编码的指针
const jbyte *str = (const jbyte *)env->GetStringUTFChars( instring, JNI_FALSE );
std::cout << str;
// 通知虚拟机本地代码不再需要通过str访问Java字符串
env->ReleaseStringUTFChars( instring, (const char *)str );
return;
}
#include <iostream>
// 与hellojni_HelloWorld.h中函数声明相同
JNIEXPORT void JNICALL Java_hellojni_HelloWorld_SayHello(JNIEnv * env, jobject arg,
jstring instring)
{
// 从instring字符串取得指向字符串UTF编码的指针
const jbyte *str = (const jbyte *)env->GetStringUTFChars( instring, JNI_FALSE );
std::cout << str;
// 通知虚拟机本地代码不再需要通过str访问Java字符串
env->ReleaseStringUTFChars( instring, (const char *)str );
return;
}
/*
* @(#)Main.java
*
* Copyright 2007 School of Software, Yunnan University.
* All rights reserved
*/
package hellojni;
/**
* 测试JNI调用
* @author 88250
* @version 1.0.0.0, Jan 31, 2008
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.SayHello("Hello World!");
}
}
* @(#)Main.java
*
* Copyright 2007 School of Software, Yunnan University.
* All rights reserved
*/
package hellojni;
/**
* 测试JNI调用
* @author 88250
* @version 1.0.0.0, Jan 31, 2008
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HelloWorld hw = new HelloWorld();
hw.SayHello("Hello World!");
}
}