代码是网上帖子找的,感觉比其他简单易懂,借鉴一下。
Java代码
- 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代码
- #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\
C工程:Sample1
编译方法见:http://425093014.blog.51cto.com/5109559/1181233
Release后把Sample1.dll放入JDK的bin目录下,如:C:\Program Files\Java\jdk1.7.0_15\bin
cmd进入D:\Java\,使用命令java Sample1
转载于:https://blog.51cto.com/425093014/1181418