JNI(Java Native Interface)是Java环境和本地环境通信的接口工具,让两者可以互相通信。
JNI的Windows开发环境
J2SDK(我用的是J2SDK1.4以上)
VC++7.0即(VS.net 2003)
顺序:
1、编写Java文件
2、编译Java文件
3、执行javah程序生成c/c++头文件(.h)
4、编写C/C++实现文件
5、编译C/C++实现文件
6、调用,
各部分的参考代码:
1、HelloWorld.java
package demo;
public class HelloWorld {
public native void displayHelloWorld(String s);
public native String c2java(byte[] mobileNo, byte[] smContent, String str);
public native int get();
public native void set(int i);
public native int getIntLength(int [] ii);
public native void getException()throws Exception;
static {
System.loadLibrary("hello");
}
public static void main(String[] args) throws Exception{
HelloWorld hw = new HelloWorld();
hw.displayHelloWorld("3 ge 1");
hw.set(90);
System.out.println(hw.get());
System.out.println("from c == " + hw.c2java(new byte[]{'h', 'o', '/0'}, new byte[]{'y', 'p', '/0'}, "Leemaasn"));
System.out.println("app1");
System.out.println("app21");
System.out.println("app31");
System.out.println("/nDobule is :" + hw.getIntLength(new int []{1, 2, 3, 4}));
hw.getException();
}
}
2、编译HelloWorld.java,使用Ant来编译,脚本如下:
<javac
srcdir="${Src.Java.Dir}"
destdir="${Classes.Dir}"
classpathref="Compile.Lib.Path"
>
<include name="**/*.java" />
</javac>
3、使用JavaH来生成头文件,同样使用ant脚本
<exec dir="${Classes.Dir}" executable="javah.exe" output="${Build.Dir}/javah_result.txt">
<arg line="-jni demo.HelloWorld" />
</exec>
4、编写C/C++实现文件(HelloWorldImpl.c)
#include <jni.h>
#include "demo_HelloWorld.h"
#include <stdio.h>
#include <string.h>
int j = 0;
JNIEXPORT void JNICALL
Java_demo_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj, jstring i)
{
char s[]={'a', 'b', 'c'};
char *c = (*env)->GetStringUTFChars(env, i, 0);
char cc [128];
strcpy(cc, c);
(*env)->ReleaseStringUTFChars(env, i, c);
printf("from java;;;%s", cc);
/**
printf(i);
char *str = (*env)->GetStringUTFChars(env, string, 0);
char cap[128];
strcpy(cap, str);
(*env)->ReleaseStringUTFChars(env, string, str);
printf(str);
*/
return;
}
JNIEXPORT jstring JNICALL Java_demo_HelloWorld_c2java
(JNIEnv * env, jobject obj, jbyteArray mobileno, jbyteArray smscontent, jstring str0)
{
jstring jstr;
char str[]={'a', 'b', 'c'};
char cap[128];
const char *str1 = (*env)->GetStringUTFChars(env, str0, 0);
char * pSmscontent ;
//jsize theArrayLengthJ = (*env)->GetArrayLength(env,mobileno);
jbyte * arrayBody = (*env)->GetByteArrayElements(env,mobileno,0);
char * pMobileNo = (char *)arrayBody;
printf("[%s]/n ", pMobileNo);
//jsize size = (*env)->GetArrayLength(env,smscontent);
arrayBody = (*env)->GetByteArrayElements(env,smscontent,0);
pSmscontent = (char *)arrayBody;
printf("<%s>/n", pSmscontent);
// return SmsSend(pMobileNo,pSmscontent);
jstr=(*env)->NewStringUTF(env, str);
strcpy(cap, str1);
printf("from java ==%s", cap);
return jstr;
}
JNIEXPORT jint JNICALL Java_demo_HelloWorld_get
(JNIEnv * env, jobject obj)
{
return j;
}
/*
* Class: demo_HelloWorld
* Method: set
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_demo_HelloWorld_set
(JNIEnv * env, jobject obj, jint i)
{
j = i;
}
JNIEXPORT jint JNICALL Java_demo_HelloWorld_getIntLength
(JNIEnv * env, jobject obj, jintArray iarr)
{
jsize len = (*env)->GetArrayLength(env, iarr);
int i;
int *ielement = (*env)->GetIntArrayElements(env, iarr, 0);
printf("Int array length is %d", len);
for (i=0; i<len; i++)
printf("/nAt pos %d is %d.", i, ielement[i]);
(*env)->ReleaseIntArrayElements(env, iarr, ielement, 0);
return len*2;
}
JNIEXPORT void JNICALL Java_demo_HelloWorld_getException
(JNIEnv * env, jobject obj)
{
/* Create the Throwable object. */
jclass cls = (*env)->FindClass(env, "java/io/IOException");
jmethodID mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
jthrowable e = (*env)->NewObject(env, cls, mid);
/* Now throw the exception */
(*env)->Throw(env, e);
return;
}
5、编译C/C++实现文件(用Ant加上一些类库)
<copy todir="${Classes.Dir}">
<fileset dir="${Lib.C.Dir}">
<include name="**/*.lib" />
<include name="**/*.dll" />
</fileset>
</copy>
<exec dir="${Classes.Dir}" executable="${CL.Path}" output="${Build.Dir}/build_dll_result.txt" >
<arg line="-I${Java.Include.Dir} -I${Java.Include.Dir}/win32 -I${Src.Include.Dir} -I${Classes.Dir} -LD ${Src.C.Dir}/HelloWorldImpl.c -Fehello.dll" />
</exec>
6、最后运行:
<java classname="demo.HelloWorld" dir="${Classes.Dir}" fork="true">
<arg value="-h" />
<classpath>
<pathelement path="${Classes.Dir}" />
</classpath>
</java>