代碼是網上帖子找的,感受比其餘簡單易懂,借鑑一下。java
Java代碼ide
- public class Sample1{
- public native String stringMethod(String text);
- public static void main(String[] args)
- {
- System.loadLibrary("Sample1");
- Sample1 sample = new Sample1();
- String text = sample.stringMethod("Thinking");
- System.out.println("stringMethod:"+text);
- }
- }
C代碼spa
- #include <D:\Java\Sample1.h>
- #include<string.h>
- #include <ctype.h>
- JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv *env, jobject obj,
- jstring string){
- int i;
- const char *str = (*env)->GetStringUTFChars(env,string,0);
- char cap[128];
- strcpy(cap,str);
- (*env)->ReleaseStringUTFChars(env,string,str);
- for(i= 0; i<strlen(cap); i++){
- *(cap+i)=(char)toupper(*(cap+i));
- }
- return (*env)->NewStringUTF(env,cap);
- }
Java代碼在:D:\Java\blog
C工程:Sample1get
編譯方法見:http://425093014.blog.51cto.com/5109559/1181233cmd
Release後把Sample1.dll放入JDK的bin目錄下,如:C:\Program Files\Java\jdk1.7.0_15\binstring
cmd進入D:\Java\,使用命令java Sample1it