Solved

NewStringUTF function does not print result on console

Posted on 2014-03-20
1
468 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 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now