• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning String to Java from C via JNI

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic