JNI crashes JVM (from C) -- openjdk 8

Xen list at xenhideout.nl
Mon Apr 23 10:14:35 UTC 2018


Hi,

I am new to using JNI but I don't know where I can get any help.

On my openjdk-8 Linux system, the example at 
https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.html

is in C++ but when run the GetStaticMethodID call crashes the JVM.

However when I change it to C, already the FindClass call crashes the 
JVM.

This fragment includes

JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env;       /* pointer to native method interface */

in C++, in C I change it to

JavaVM *jvm;
JNIEnv env;

to make it compile.

I compile with include dirs of

/usr/lib/jvm/java-8-openjdk-amd64/include and
/usr/lib/jvm/java-8-openjdk-amd64/include/linux

And link with

/usr/lib/jvm/java-8-openjdk-amd64/jre/lib
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/server

and test.c -ljava -ljvm

Consequently, I run with LD_LIBRARY_PATH=...:... with those values.

The program runs, but there is always a JVM crash. I was surprised to 
see it pass the FindClass call with C++.

I have not tested other JDKs or JREs.

The Main class is as simple as it gets, as per the example:

public class Main {
     public static void test(int i) {
         System.out.println(i);
     }
}

But honestly, the C version always crashes the JVM, no matter what class 
I give it; even non-existent classes always crash it.

I can attach verbosity (-verbose:jni) but it lists no errors anywhere; 
or -Xcheck:jni, but that doesn't change the output.

I can attach the complete output, but I was wondering if this is a known 
issue?

Am I doing something wrong?

This is Oracle's minimal example; the code compiles and can find 
everything.

#include <jni.h>       /* where everything is defined */

int main() {
     JavaVM *jvm;       /* denotes a Java VM */
     JNIEnv env;       /* pointer to native method interface */
     JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
     JavaVMOption options[1];
     options[0].optionString = "-Djava.class.path=/usr/lib/java";
     vm_args.version = JNI_VERSION_1_6;
     vm_args.nOptions = 1;
     vm_args.options = options;
     vm_args.ignoreUnrecognized = 0;
     /* load and initialize a Java VM, return a JNI interface
      * pointer in env */

     JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

     /* invoke the Main.test method using the JNI */

     jclass cls = env->FindClass(&env, "Main");            // <--- C 
crashes here
     jmethodID mid = env->GetStaticMethodID(&env, cls, "test", "(I)V");   
  // <--- C++ here
     env->CallStaticVoidMethod(cls, mid, 100);             // <--- never 
reached
     /* We are done. */
     (*jvm)->DestroyJavaVM(jvm);
}



I have tested this on CentOS 7.4 as well, with versions 1.8.0, 1.7.0 and 
1.6.0, all giving the same error messages:

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000000000000000, pid=24945, 
tid=0x00007f5eeab0f740
#
# JRE version: OpenJDK Runtime Environment (8.0_161-b14) (build 
1.8.0_161-b14)
# Java VM: OpenJDK 64-Bit Server VM (25.161-b14 mixed mode linux-amd64 
compressed oops)
# Problematic frame:
# C  0x0000000000000000

The others are the same.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: hs_err_pid25637.log
URL: <http://mail.openjdk.java.net/pipermail/discuss/attachments/20180423/1a368b98/hs_err_pid25637-0001.log>


More information about the discuss mailing list