Solved

NewStringUTF function does not print result on console

Posted on 2014-03-20
1
486 Views
Last Modified: 2014-03-27
I am writing a native method (from java to C).  
Actual C Code:-
	JNIEXPORT jstring JNICALL Java_ClassName_pspLoadFiles(JNIEnv *env, jobject obj, jstring ursapath, jstring editpath){
	
	const char * cursapath = (*env)->GetStringUTFChars(env, ursapath, NULL);
	const char * ceditpath = (*env)->GetStringUTFChars(env, editpath, NULL);
	const char * cresult = (const char *) (uvsdkLoadFiles(cursapath, ceditpath));
//cresult does show value on console
	printf("\n   status %s\n", cresult);

	jstring jresult = (*env)->NewStringUTF(env, cresult);
	printf("\n   does not print On Console %s\n", (*env)->NewStringUTF(env, cresult));
	(*env)->ReleaseStringUTFChars(env, ursapath, cursapath);
	(*env)->ReleaseStringUTFChars(env, editpath, ceditpath);
        printf("\n   jresult %s\n", jresult); // does not print on console
	return jresult;	//valid values of jresult is returned back to Java
}

Open in new window

Code in Header File
JNIEXPORT jstring JNICALL Java_ClassName_pspLoadFiles
  (JNIEnv *, jobject, jstring, jstring);

Open in new window


This line does not print values on console:-
(*env)->NewStringUTF(env, cresult);

Environment is AIX64 bit and Java 5.
0
Comment
Question by:jgdvishnu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39944433
This line does not print values on console:-
(*env)->NewStringUTF(env, cresult);
That sounds about right. The result of that function call is of type "jstring" (not a pointer to character data) and so it makes sense that you can't just print it to the console as a normal c string. You should be able to use the GetStringUTFChars function call like you are already using for other jstring's to convert it to a c string to print to the console, ie...
	JNIEXPORT jstring JNICALL Java_ClassName_pspLoadFiles(JNIEnv *env, jobject obj, jstring ursapath, jstring editpath){
	
	const char * cursapath = (*env)->GetStringUTFChars(env, ursapath, NULL);
	const char * ceditpath = (*env)->GetStringUTFChars(env, editpath, NULL);
	const char * cresult = (const char *) (uvsdkLoadFiles(cursapath, ceditpath));
//cresult does show value on console
	printf("\n   status %s\n", cresult);

	jstring jresult = (*env)->NewStringUTF(env, cresult);
	const char * c_jresult = (*env)->GetStringUTFChars(env, jresult, NULL);
	printf("\n   SHOULD print on Console %s\n", c_jresult);
	(*env)->ReleaseStringUTFChars(env, jresult, c_jresult);
	(*env)->ReleaseStringUTFChars(env, ursapath, cursapath);
	(*env)->ReleaseStringUTFChars(env, editpath, ceditpath);
        //printf("\n   jresult %s\n", jresult); // does not print on console
	return jresult;	//valid values of jresult is returned back to Java
}

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question