Solved

NewStringUTF function does not print result on console

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parallel rsync issues with Ubuntu 1 39
program arguments vs VM arguments 4 26
python - find anything after $ question. 9 45
Setting variables in a stored procedure 5 46
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Fine Tune your automatic Updates for Ubuntu / Debian
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 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