Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3337659
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: Java

2011-03-31 23:04:14

  • D_string.java 文件
Java代码
  1. package com.ldq.d_string;   
  2.   
  3. public class D_string {   
  4.     static {   
  5.         System.loadLibrary("Case");   
  6.     }   
  7.   
  8.     public native String getCase(String string);   
  9.   
  10.     /**  
  11.      * @param args  
  12.      */  
  13.     public static void main(String[] args) {   
  14.         // TODO Auto-generated method stub   
  15.         D_string d = new D_string();   
  16.         String s = "heLLo";   
  17.         System.out.println("before change: " + s);   
  18.         System.out.println("after change: " + d.getCase(s));   
  19.     }   
  20.   
  21. }  
package com.ldq.d_string; public class D_string { static { System.loadLibrary("Case"); } public native String getCase(String string); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub D_string d = new D_string(); String s = "heLLo"; System.out.println("before change: " + s); System.out.println("after change: " + d.getCase(s)); } }

 

  • javah -jni com.ldq.d_string.D_string 生成文件 com_ldq_d_string_D_string.h
C代码
  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include    
  3. /* Header for class com_ldq_d_string_D_string */  
  4.   
  5. #ifndef _Included_com_ldq_d_string_D_string   
  6. #define _Included_com_ldq_d_string_D_string   
  7. #ifdef __cplusplus   
  8. extern "C" {   
  9. #endif   
  10. /*  
  11.  * Class:     com_ldq_d_string_D_string  
  12.  * Method:    getCase  
  13.  * Signature: (Ljava/lang/String;)Ljava/lang/String;  
  14.  */  
  15. JNIEXPORT jstring JNICALL Java_com_ldq_d_1string_D_1string_getCase   
  16.   (JNIEnv *, jobject, jstring);   
  17.   
  18. #ifdef __cplusplus   
  19. }   
  20. #endif   
  21. #endif  
/* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class com_ldq_d_string_D_string */ #ifndef _Included_com_ldq_d_string_D_string #define _Included_com_ldq_d_string_D_string #ifdef __cplusplus extern "C" { #endif /* * Class: com_ldq_d_string_D_string * Method: getCase * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_ldq_d_1string_D_1string_getCase (JNIEnv *, jobject, jstring); #ifdef __cplusplus } #endif #endif

 

  • 编写源代码文件 Case.cpp
Cpp代码
  1. #include    
  2. #include "com_ldq_d_string_D_string.h"   
  3.   
  4. JNIEXPORT jstring JNICALL Java_com_ldq_d_1string_D_1string_getCase (JNIEnv *env, jobject obj, jstring string)   
  5. {   
  6.     const char *s=env->GetStringUTFChars(string,0);   
  7.     char s1[16],s2[16];   
  8.     strcpy(s1,s);   
  9.     strcpy(s2,s);   
  10.     strupr(s1);   
  11.     strlwr(s2);   
  12.     strcat(s1,"_");   
  13.     strcat(s1,s2);   
  14.     env->ReleaseStringUTFChars(string,s);   
  15.     return env->NewStringUTF(s1);   
  16. }  
#include #include "com_ldq_d_string_D_string.h" JNIEXPORT jstring JNICALL Java_com_ldq_d_1string_D_1string_getCase (JNIEnv *env, jobject obj, jstring string) { const char *s=env->GetStringUTFChars(string,0); char s1[16],s2[16]; strcpy(s1,s); strcpy(s2,s); strupr(s1); strlwr(s2); strcat(s1,"_"); strcat(s1,s2); env->ReleaseStringUTFChars(string,s); return env->NewStringUTF(s1); }

 

  • cl /LD Case.cpp 编译生成 Case.dll 文件
  • 拷贝 Case.dll 文件到工程目录,运行 Java 程序 
运行结果
before change: heLLo
after change: HELLO_hello
阅读(3075) | 评论(0) | 转发(0) |
0

上一篇:JNI 返回对象

下一篇:JNI 中的 Signature

给主人留下些什么吧!~~