JNI开发环境

9 篇文章 0 订阅

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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值