本文介绍了将字符串从Java传递到JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个字符串传递给我正在写的JNI,它必须分配给一个const char *。
下面提到我是如何做到的:
I want to pass a string into the JNI I am writing which have to be assigned to a const char*.The below mentioned is how I have done it:
JNI...(...,jstring jstr...){
const char* str = env->GetStringUTFChars(jstr,0);
env->ReleaseStringUTFChars(str,jstr,0);
}
但如果我在分配给constf * str之后打印它我所看到的jstring与我直接在JNI和printf中直接分配str值的情况不同。
But if i printf the const char* str after assigning to the jstring what I see is different as compared to when I assigned the str value directly in the JNI and printf from there.
这是正确的方法吗?或者是否还有其他方法可以将字符串从java分配给JNI中的const char *?
Is this the correct way to do? Or is there any other way to assign a string from java to const char* in JNI ?
推荐答案
java代码
public static native double myMethod( String path);
C代码
JNIEXPORT jdouble JNICALL Java_your_package_structure_className_myMethod
(JNIEnv * env, jobject jobj, jstring path) {
char * path;
path = (*env)->GetStringUTFChars( env, path , NULL ) ;
- Updated link
这篇关于将字符串从Java传递到JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!