This discussion is archived
2 Replies Latest reply: Apr 25, 2012 4:11 PM by jschellSomeoneStoleMyAlias RSS

JVM dies when JNI native code causes a SIGABRT from assertions

932922 Newbie
Currently Being Moderated
Hi,

I am wondering whether there is a way to prevent the JVM from dying when the JNI native code hits an assertion.


#include "NativeTest.h"
#include <assert.h>

JNIEXPORT jstring JNICALL Java_NativeTest_sayHello (JNIEnv *env, jobject thisobject, jstring js)
{

assert(0);
return js;
}


Calling this code from Java through JNI causes a SIGABRT when assert(0) is hit. This causes the java program to terminate. Is there a way for the JVM to recover from the SIGABRT from the native code?
  • 1. Re: JVM dies when JNI native code causes a SIGABRT from assertions
    EJP Guru
    Currently Being Moderated
    Eh? C assertions are specified to cause a SIGABORT, and SIGABORT is supposed to, err, abort. That's what assertions are all about. If you don't want that effect, don't use them, or turn them off.
  • 2. Re: JVM dies when JNI native code causes a SIGABRT from assertions
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    929919 wrote:
    I am wondering whether there is a way to prevent the JVM from dying when the JNI native code hits an assertion.
    There is no way to prevent the VM from exiting if native code does anything that causes an exit.
    An assertion is only one way that can happen.

    So to prevent the VM from exiting - don't run native code. The safe way to execute OS native library is to do the following.
    1. Wrap the library in an executable.
    2. Create a communications API for the executable.
    3. Manage the executable via Java Runtime.exec/ProcessBuilder.
    4. Talk to the executable using the communications API from 2 in the java code.

    The above is safe because if the library exits it exits the executable, not the VM.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points