把java中的字符窜,传给C,让C输出这个是就会乱码,
在让C返回这个窜给java输出时,会抛出:OutOfMemoryError
代码如下:
java:
public class testNative {
/** Declaration a new method of testNative */
public static native void setString(String s );
public static native String getString();
/**
*load library
*/
static{
System.loadLibrary("HelloNative");
}
public static void main(String[] args) throws Exception{
String s = "this is a test!\n";
String sTemp;
testNative.setString(s);
sTemp = testNative.getString();
System.out.println("sTemp is: " + sTemp);
}
}C:#include "firstNative.h"
static jstring ss;
JNIEXPORT void JNICALL Java_firstNative_setString(JNIEnv* env, jclass cl, jstring jstr)
{
ss = jstr;
printf("%s \n", ss);
}JNIEXPORT jstring JNICALL Java_firstNative_getString(JNIEnv *env, jclass cl)
{
return ss;
}
结果:
D:\bak\aa\jni>java testNative
0ヘユネ・
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exce
eds VM limit
在让C返回这个窜给java输出时,会抛出:OutOfMemoryError
代码如下:
java:
public class testNative {
/** Declaration a new method of testNative */
public static native void setString(String s );
public static native String getString();
/**
*load library
*/
static{
System.loadLibrary("HelloNative");
}
public static void main(String[] args) throws Exception{
String s = "this is a test!\n";
String sTemp;
testNative.setString(s);
sTemp = testNative.getString();
System.out.println("sTemp is: " + sTemp);
}
}C:#include "firstNative.h"
static jstring ss;
JNIEXPORT void JNICALL Java_firstNative_setString(JNIEnv* env, jclass cl, jstring jstr)
{
ss = jstr;
printf("%s \n", ss);
}JNIEXPORT jstring JNICALL Java_firstNative_getString(JNIEnv *env, jclass cl)
{
return ss;
}
结果:
D:\bak\aa\jni>java testNative
0ヘユネ・
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exce
eds VM limit
printf("JNITEST.DLL, print: %s\n", ss);
env->ReleaseStringUTFChars(jstr, ss);