120

I have passed a URL string from Java to C code as jstring data type through the use of JNI. And my library method needs a char * as url.

How can I convert jstring in char * ?

P.S.: Is there any advantage of using jcharArray in C? (i.e. Passing char [] instead of string in native method)

Universal Electricity
  • 775
  • 1
  • 12
  • 26
Prasham
  • 6,646
  • 8
  • 38
  • 55

2 Answers2

242

Here's a a couple of useful link that I found when I started with JNI

http://en.wikipedia.org/wiki/Java_Native_Interface
http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/functions.html

concerning your problem you can use this

JNIEXPORT void JNICALL Java_ClassName_MethodName(JNIEnv *env, jobject obj, jstring javaString)   
{
   const char *nativeString = env->GetStringUTFChars(javaString, 0);

   // use your string

   env->ReleaseStringUTFChars(javaString, nativeString);
}
Stoica Mircea
  • 782
  • 10
  • 22
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • 1
    is it necessary to keep `nativeString` constant? – Prasham Nov 15 '10 at 07:07
  • 5
    if you check the second link, the prototype of the function GetStringUTFChars is: const jbyte* GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy); so you don't really have a choise – Jason Rogers Nov 15 '10 at 07:26
  • no problems, I'm getting to the end of my project with JNI but I know how much I struggled to start with – Jason Rogers Nov 15 '10 at 09:22
  • 2
    I think it's worth noting that the technique outlined here (and on the Wikipedia page) uses modified UTF-8 encoding, which may not work in all situations. See http://developer.android.com/guide/practices/jni.html#UTF_8_and_UTF_16_strings – cqcallaw Oct 01 '12 at 01:59
  • I think this approach works only in the case when all characters are encoded with 1 byte. But an UTF8 string can have characters encoded with 2 and 4 bytes too. – WindRider Dec 05 '13 at 20:05
  • 2
    Thanks, that work but interestingly const char* can not convert into char* :). – CoDe Dec 04 '15 at 13:10
  • 1
    @CoDe Technically, it could with some magic, but that is best avoided; it is probably declared `const` for a reason. The correct response to your problem is to `strcpy` from the `const char*` into a `char*`. That should provide you with the mutable `char*` that you want. – Loduwijk Jun 20 '17 at 20:44
53

Thanks Jason Rogers's answer first.

In Android && cpp should be this:

const char *nativeString = env->GetStringUTFChars(javaString, nullptr);

// use your string

env->ReleaseStringUTFChars(javaString, nativeString);

Can fix this errors:

1.error: base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'

2.error: no matching function for call to '_JNIEnv::GetStringUTFChars(JNIEnv*&, _jstring*&, bool)'

3.error: no matching function for call to '_JNIEnv::ReleaseStringUTFChars(JNIEnv*&, _jstring*&, char const*&)'

4.add "env->DeleteLocalRef(nativeString);" at end.

Dark
  • 227
  • 3
  • 9
kangear
  • 2,493
  • 2
  • 31
  • 44