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