简体   繁体   English

Java运行时环境检测到致命错误

[英]Fatal Error Detected by the Java Runtime Environment

I am trying to run a simple Java program using C++ and wrote the following code- 我正在尝试使用C ++运行一个简单的Java程序并编写以下代码 -

1 #include <jni.h>
2 #include <stdio.h>
3
4 JNIEnv * create_vm() {
5     JavaVM *jvm;
6     JNIEnv *env;
7     JavaVMInitArgs vm_args;
8
9     JavaVMOption options[1];
10
11     options[0].optionString = "-Djava.class.path=~/openbr/java/JavainC";
12     vm_args.version = JNI_VERSION_1_6;
13     vm_args.nOptions = 1;
14     vm_args.options = options;
15     vm_args.ignoreUnrecognized = JNI_FALSE;
16
17     int res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
18     return env;
19 }
20
21 void run_java(JNIEnv* env) {
22     jclass cls;
23     jmethodID mid;
24     jint square;
25
26     cls = env->FindClass("javaTest");
27     mid = env->GetStaticMethodID(cls, "square", "(I)I");
28     square = env->CallStaticIntMethod(cls, mid, 5);
29     printf("Result of square: %d\n", square);
30 }
31
32 int main() {
33     JNIEnv* env = create_vm();
34     run_java(env);
35     printf("running some code");
36     //jvm->DestroyJavaVM();
37     return 0;
38 }

My Java Code looks like- 我的Java代码看起来像 -

1 public class javaTest {
2     public static int square(int n) {
3         return n*n;
4     }
5 }
6

My code compiled fine but when I tried to run it I got this error message- 我的代码编译得很好但是当我试图运行它时,我得到了这个错误信息 -

# A fatal error has been detected by the Java Runtime Environment: 
#
#  SIGSEGV (0xb) at pc=0x00007fc3038f984f, pid=12689, tid=140475565049664
#
# JRE version: 7.0_21-b02
# Java VM: OpenJDK 64-Bit Server VM (23.7-b01 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.so+0x53284f]  jni_CallStaticIntMethodV+0x4f
#
# 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/jcheney/openbr/java/JavainC/hs_err_pid12689.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   https://bugs.launchpad.net/ubuntu/+source/openjdk-7/
#
Aborted

The error printout it gave me said- 它给我的错误打印输出说 -

---------------  T H R E A D  ---------------

Current thread (0x00000000011be800):  JavaThread "main" [_thread_in_vm, id=12689, stack
(0x00007fff54814000,0x00007fff54914000)]

siginfo:si_signo=SIGSEGV: si_errno=0, si_code=1 (SEGV_MAPERR), si_addr=0x0000000000000000

Registers:
RAX=0x00007fc303c4a138, RBX=0x00000000011be800, RCX=0x00007fff549127c0, RDX=0x00000000011bf318
RSP=0x00007fff549126e0, RBP=0x00007fff54912790, RSI=0x00000000011be800, RDI=0x00000000011be800
R8 =0x00007fc3038f9800, R9 =0x746f7073746f682f, R10=0x6168732f6372732f, R11=0x0000000000000246
R12=0x00000000011be9d8, R13=0x0000000000000000, R14=0x00007fff549127c0, R15=0x0000000000000000
RIP=0x00007fc3038f984f, EFLAGS=0x0000000000010287, CSGSFS=0x0000000000000033,    
ERR=0x0000000000000004
TRAPNO=0x000000000000000e

Top of Stack: (sp=0x00007fff549126e0)
0x00007fff549126e0:   00007fff549126c8 0000000800000002
0x00007fff549126f0:   00007fff54912701 0000000000000000
0x00007fff54912700:   00007fff54912730 00007fc3038125ac
0x00007fff54912710:   00000000011be800 00000000011be800
0x00007fff54912720:   00000000011be800 00000000011bf310
0x00007fff54912730:   00007fff0000063d 00007fc303c4a138
0x00007fff54912740:   0000000000000000 0000000000400901
0x00007fff54912750:   00000000011be800 0000000000000000
0x00007fff54912760:   00000000011be800 0000000000400906
0x00007fff54912770:   0000000000000000 00000000004004d0
0x00007fff54912780:   00007fff549129d0 0000000000000000
0x00007fff54912790:   00007fff54912890 00000000004007cd
0x00007fff549127a0:   0000000000000000 0000000000000000
0x00007fff549127b0:   0000000000000000 00000000011be9d8
0x00007fff549127c0:   0000003000000018 00007fff549128a0
0x00007fff549127d0:   00007fff549127e0 0000000000400901
0x00007fff549127e0:   0000000000000000 0000000000000000
0x00007fff549127f0:   00007fff54912860 0000000000000005
0x00007fff54912800:   00007fc303c3ae8f 746f7073746f682f
0x00007fff54912810:   00007fff000000a8 00007fc303c68e68
0x00007fff54912820:   00000000011be800 00007fff549128c0
0x00007fff54912830:   00000000011be800 00007fc303903233
0x00007fff54912840:   0000000000000000 00000000004004d0
0x00007fff54912850:   00007fff549129d0 0000000000000000
0x00007fff54912860:   00007fff54912890 0000000000400712
0x00007fff54912870:   0000000000400901 0000000000400906
0x00007fff54912880:   0000000000000000 00000000011be9d8
0x00007fff54912890:   00007fff549128d0 000000000040065b
0x00007fff549128a0:   00000000004008d0 00000000011be9d8
0x00007fff549128b0:   00007fff54912930 0000000000000000
0x00007fff549128c0:   0000000000000000 00000000004007e0
0x00007fff549128d0:   00007fff549128f0 0000000000400691

Any ideas on what is going on? 关于发生了什么的任何想法?

Thanks 谢谢

My guess -- and it's only a guess at this point -- is that one of the methods FindClass or GetStaticMethodId is not succeeding for some reason, and since you're not checking for errors, your program eventually crashes trying to call a method through a bad method pointer. 我的猜测 - 这只是猜测 - 是FindClassGetStaticMethodId方法FindClass由于某种原因没有成功,并且由于你没有检查错误,你的程序最终崩溃试图通过调用方法一个糟糕的方法指针。 Minimally, check the return values of both of these methods against NULL , and fail if that's what they return. 最小化,检查这两个方法的返回值是否为NULL ,如果返回它们则失败。

At @TimBender's urging, I looked at your code again, and I agree -- it's probably FindClass . 在@TimBender的催促下,我再次看了你的代码,我同意 - 它可能是FindClass You definitely can't include a ~ in your classpath -- only the shell knows how to interpret that, and that parameter won't go through a shell before the JVM sees it. 你绝对不能在你的classpath包含一个~只有shell知道如何解释它,并且该参数在JVM看到它之前不会通过shell。 Use a full, absolute path to the location of the class -- the root of the package hierarchy, actually. 实际上,使用类的位置的完整绝对路径 - 包层次结构的根。

Based on limited research, I'm 99% sure that the problem is with the FindClass declaration. 基于有限的研究,我99%确定问题在于FindClass声明。 FindClass accepts as the second argument a fully qualified class name. FindClass接受第二个参数作为完全限定的类名。 Fully qualified meaning the package name must be included before the class name. 完全限定意味着包名必须包含在类名之前。 I'm assuming that javaTest is not in the default package, or if it is that there is still some convention for referencing it. 我假设javaTest不在默认包中,或者如果它仍然存在一些引用它的约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM