Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NewStringUTF function does not print result on console

Posted on 2014-03-20
1
Medium Priority
?
496 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
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

916 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