aspose file tools*
The moose likes Java in General and the fly likes Returning String  to Java from C via JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Returning String  to Java from C via JNI" Watch "Returning String  to Java from C via JNI" New topic
Author

Returning String to Java from C via JNI

Amy Smith
Greenhorn

Joined: Jul 26, 2001
Posts: 24
Hi all,
I am getting a SIGSEGV 11 segmentation violation when running the following code. It is caused when I try to return a String from the C program, otherwise it works OK. (Solaris)
I have read a couple of things about Dereferenced variables, but do not know how that all translates into code...
The JNI tutorial at Sun is great if you don't need to pass parameters!
Thanks.
--Amy--
Java:::::::::::::::::::::::::::::::::::::::::::::::::::::::
public class HelloWorld {
public static native String displayHelloWorld(String name);
static {
System.loadLibrary("hello");
}

public static void main(String[] args) {
String person = new String("Amy");
String answer = displayHelloWorld(person);
System.out.println("Answer: " + answer);
}
}
C code:::::::::::::::::::::::::::::::::::::::::::::::::::::
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT jstring JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj, jstring person)
{
char done[5] = "DONE";
const char *ptr_person = (*env)->GetStringUTFChars(env, person, 0);
printf("Hello World\n");
puts(ptr_person);
ptr_person = "DONE";
(*env)->ReleaseStringUTFChars(env,person,ptr_person);
return (*env)->NewString(env,ptr_person,4);
}
H file:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: (Ljava/lang/String Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif

Amy Smith<br />Java Developer<br />Haworth, Inc.<br />amy.smith@haworth.com
Amy Smith
Greenhorn

Joined: Jul 26, 2001
Posts: 24
OK... I got it. Make a new jstring String object based on the C char array then free the C char array. See below.
Other more elegant solutions are welcome.
Java:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
JNIEXPORT jstring JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj, jstring person)
{
char done[5] = "DONE";
const char* ptr_person;
jstring answer;
ptr_person = (const char *)(*env)->GetStringUTFChars(env, person, 0);
printf("Hello World\n");
puts(ptr_person);
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning String to Java from C via JNI