Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6344049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:33:49+00:00 2026-05-24T20:33:49+00:00

I am using JNI to extend my API in Java, in order to make

  • 0

I am using JNI to extend my API in Java, in order to make it accessible through C++ (I am creating wrapper classes). I am experienced in Java but new to C++.

Now, I am creating a wrapper in C++ for a Java function getString() which returns a String. I am using GetStringUTFChars to get the C string from the Java String but I don’t know where to use ReleaseStringUTFChars which releases the C string. Here is my code (aClass and aClassInstance are private date members inside the wrapper class):

const char* getString() {
jmethodID methodID = env->GetMethodID(aClass,
        "methodName",
        "()Ljava/lang/String;");
if (methodID == NULL) {
    cout << "--methodID = NULL";
    exit(0);
}
jstring jstringResult = (jstring) env->CallObjectMethod(aClassInstance, methodID);
const char* result = env->GetStringUTFChars(jstringResult, NULL);
// env->ReleaseStringUTFChars(jstringResult, result); 
return result;
}

If I remove the // and use ReleaseStringUTFChars, result will be released and I will no longer be able to return it, right? Should I copy result to another char array before releasing it?

Thanks, any help is welcome!

K

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:33:49+00:00Added an answer on May 24, 2026 at 8:33 pm

    Mahesh is right

    std::string getString()
    {
        jmethodID methodID = env->GetMethodID(aClass,
            "methodName",
            "()Ljava/lang/String;");
        if (methodID == NULL)
        {
            cout << "--methodID = NULL";
            exit(0);
        }
        jstring jstringResult = (jstring) env->CallObjectMethod(aClassInstance, methodID);
        const char* result = env->GetStringUTFChars(jstringResult, NULL);
        std::string result2(result);
        env->ReleaseStringUTFChars(jstringResult, result); 
        return result2;
    }
    

    std::string takes copies your characters for you and will automatically free when needed. Using raw pointers in C++ is generally frowned upon. Coming from Java you probably have little idea of the problems they can cause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently implementing a .NET wrapper for a Java library by using JNI to
I started using JNI to call Java classes from C++ some weeks ago and
I'm creating Java wrappers for some C code using JNI. There are dependencies on
I am creating a simple video editing application using Java, JNI, C, and FFmpeg.
I'm using JNI to access a native C file through my java program. I've
I'm developing a Java wrapper for a C++ project using Jni. As I'm having
I am using JNI to call my C++ function from Java. One of the
I have a C program that stores some object in java store using JNI.
How can we access static enum fields using JNI invocation API I am trying
I'm trying to create a Qt main windows from Java using JNI directly and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.