c++文件开头加一句
#pragma execution_character_set("utf-8")
即可。
c++:
#pragma execution_character_set("utf-8")//设置字符编码
#include<JniTest.h>
#include<iostream>
using namespace std;
JNIEXPORT void JNICALL Java_JniTest_sayHello
(JNIEnv* env, jclass)
{
cout << "你好,Java" << endl;
cout << "这里是c++的输出哦!" << endl;
}
JAVA:
public class JniTest {
static {
System.load("...\hello.dll");
}
public static native void sayHello();
public static void main(String[] args) {
sayHello();
}
}
运行结果:
将字符串返回到JAVA:
C++:
#pragma execution_character_set("utf-8")//改变字符编码
#include<JniTest.h>
#include<iostream>
using namespace std;
JNIEXPORT jstring JNICALL Java_JniTest_getStringFormCpp
(JNIEnv* env, jclass)
{
string result = "我是C++";
return env->NewStringUTF(result.c_str());
}
JAVA:
public class JniTest {
static {
System.load("...\\hello.dll");
}
public static native String getStringFormCpp();
public static void main(String[] args) {
System.out.println(getStringFormCpp());
}
}
运行结果: