Solved

NewStringUTF function does not print result on console

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

929 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

15 Experts available now in Live!

Get 1:1 Help Now