java String,Jni jstring, utf-8/unicode interoperate sample code

继 前一篇 jni基本原理后,这里写了一个sample code,有关java String object 如何传到 jni jstring,native code 如何取得 java String object 的utf-8, unicode character,native 的utf-8, unicode character如何传到 java String等操作。

software enviroment: ubuntu 8.10+jdk 1.5,+gcc 4.3.2

1. after editing java code, compile it to class file: 

javac HelloWorld.java 

public class HelloWorld {
	public static String java_str="Java String";

	static {
		System.loadLibrary("Hello");
	}

	public native void DisplayHello();

	public native String PassToJni(String java_str);
	
	public static void main(String[] args){
		HelloWorld obj=new HelloWorld();
		obj.DisplayHello();
		String ret = obj.PassToJni(java_str);
		System.out.println(ret);
	}	
}

2. to generate jni header file:

      javah HelloWorld

then you will get a jni header file named HelloWorld.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    DisplayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_DisplayHello
  (JNIEnv *, jobject);

/*
 * Class:     HelloWorld
 * Method:    PassToJni
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_HelloWorld_PassToJni
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

3.  new a c-source code file to implement the jni header file

     vim helloworldImpl.c

#include <stdio.h>
#include "HelloWorld.h"


JNIEXPORT void JNICALL Java_HelloWorld_DisplayHello
  (JNIEnv *env, jobject obj)
  {
  	printf("in native code,say hello\n");
  }

JNIEXPORT jstring JNICALL Java_HelloWorld_PassToJni
  (JNIEnv *env, jobject obj, jstring str)
{
	char buf_utf8[128]="能在别人身上找到自己的一些线索bbbbbbbbbb";
	const jchar* uni_native="aaaaaa";
	// jstring--> utf-8 char
	const char *c = (*env)->GetStringUTFChars(env,str,0);
	printf("string=%s\n",c);
	printf("sizeof(char)=%d,len=%d\n",sizeof(char),(*env)->GetStringUTFLength(env,str));
	(*env)->ReleaseStringUTFChars(env,str,c);

	// jstring --> unicode char
	const jchar *uni = (*env)->GetStringChars(env,str,0);
	printf("unicode=%s\n",uni);
	printf("sizeof(char)=%d,len=%d\n",sizeof(jchar),(*env)->GetStringLength(env,str));
	(*env)->ReleaseStringChars(env,str,uni);

	// native utf-8 char --> jstring
	jstring jstr=(*env)->NewStringUTF(env,buf_utf8);
	
	// native unicode  ---> jstring
	//jstring jstr=(*env)->NewString(env,uni_native,sizeof(uni_native));
	return jstr;
	
}

4. compile c-source code to generate a share library 

     gcc -shared -I /usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include/ helloworldImpl.c -o libHello.so

5.  run java executive file linked with libHello.so library

    java -Djava.library.path=. HelloWorld 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值