Segmentation Fault when accessing java through JNI

May 6, 2015 at 11:26am
Hi guys :)

New to c++ and have come across a segmentation fault, not really sure how to debug them. Can anyone advise me to how to proceed?

Here's the code I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <jni.h>       /* where everything is defined */

int main() {
    JNIEnv *env1;
    JavaVM**  jvm1;
    JavaVMInitArgs vm_args1;
    JavaVMOption options1[2];
    options1[0].optionString = "-Djava.library.path=/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/";
    options1[1].optionString = "-Djava.class.path=.";
    vm_args1.version = JNI_VERSION_1_6;
    vm_args1.nOptions = 2;
    vm_args1.options = options1;
    vm_args1.ignoreUnrecognized = 0;


    #The segmentation fault seems to be happening on this line, as when it's commented out it runs fine
    int reAt = JNI_CreateJavaVM(jvm1, (void**)&env1, &vm_args1);


    return 0;
}


Compiling it with:

g++ -g main.cpp -I/usr/lib/jvm/java-7-oracle/include -I/usr/lib/jvm/java-7-oracle/include/linux -L/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server -ljvm

Here is the console output:

# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f719f3c132e, pid=6152, tid=140125990274880
#
# JRE version: Java(TM) SE Runtime Environment (7.0_80-b15) (build 1.7.0_80-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x63b32e]  JNI_CreateJavaVM+0xbe
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/gem/plus/console/hs_err_pid6152.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#


I have the error file as well, but the output won't fit in this post :( if the above is not enough I could probably split ip it in subsequent posts though, please let me know :)

Last edited on May 6, 2015 at 11:26am
May 6, 2015 at 12:32pm
1
2
 JavaVM**  jvm1; //uninitialised variable
int reAt = JNI_CreateJavaVM(jvm1/*trying to use it*/, (void**)&env1, &vm_args1);


It may probably be
1
2
JavaVM *jvm1;
int reAt = JNI_CreateJavaVM(&jvm1, (void**)&env1, &vm_args1);
that way the function may manipulate the pointer an set it to some allocated memory
May 6, 2015 at 1:37pm
Yeah that's done the trick! Thank you so much, think i'd best read up on pointers until I fully understand them...
May 6, 2015 at 11:36pm
eeew.... why on earth would you want to do that?
May 8, 2015 at 4:22am
ulimit -c unlimited

It told you buddy.
Topic archived. No new replies allowed.