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

  • Home
  • SEARCH
  • 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 6472499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:21:07+00:00 2026-05-25T06:21:07+00:00

I am trying to pass a String to a native c function but the

  • 0

I am trying to pass a String to a native c function but the String gets printed as 09▀30≈ß#@÷g for the String Type a String : . I don’t know what the problem is .

This is the code that i have used to do this :

JAVA CODE

class Prompt {

private native String myGetLine( String prompt ); // native method that prints a prompt and reads a line 

public static void main( String args[] ) {
Prompt prompt = new Prompt();
String input = prompt.myGetLine( "Type a String : ");
System.out.println("User Typed : " + input);
}

static {
 System.loadLibrary("StringPassing"); // StringPassing.dll
 }
}

C CODE

#include <jni.h>
#include <stdio.h>
#include "D:\UnderTest\JNI\StringPassing\Debug\Prompt.h"

JNIEXPORT jstring JNICALL Java_Prompt_myGetLine(JNIEnv *env, jobject obj, jstring str) {
char buf[128];
const jbyte *str_JVM;
str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
if( str_JVM == NULL ) return NULL;
printf("%s ",str);
(*env)->ReleaseStringUTFChars( env , str , str_JVM);
scanf("%s",buf);
return (*env)->NewStringUTF( env , buf);
}

The screen after the program ends looks like :

D:\UnderTest\JNI\StringPassing\Debug>java Prompt
09▀30≈ß#@÷gV
User Typed : V

Why don’t i get the string Type a String : on cmd when i run the program?

Also: my compiler underlines (*env) and points out that Expression must have pointer type. But when I compile I don’t get any errors. Why is that and what is it?

  • 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-25T06:21:07+00:00Added an answer on May 25, 2026 at 6:21 am

    You pass str to printf when I’m pretty sure that you wanted to pass str_JVM to it instead.

    str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
    if( str_JVM == NULL ) return NULL;
    printf("%s ",str_JVM);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to pass a string from main to another function. this string is
I'm trying to pass a string parameter in MVC3. The URL that gets generated
I'm trying to pass a string like this: {key:[value],key2:undefined,key3:undefined,key4:undefined,key5:value} to a javascript-function like this:
I'm trying to pass a String variable into my javascript function, but my variable
I'm trying to pass a string to chdir(). But I always seem to have
I'm trying to pass a string argument to a target function in a process.
I'm trying to pass an NSString by reference but it doesn't work. This is
i'm trying to pass a php defined string with spaces to a javascript function,
I'm trying to pass an encrypted query string to another URL. The following code
I am trying to pass a List<string> to a method that will take it

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.