0

我正在尝试使用 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 }

我的 Java 代码看起来像 -

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

我的代码编译得很好,但是当我尝试运行它时,我收到了这个错误消息-

# 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

它给我的错误打印输出说 -

---------------  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

关于发生了什么的任何想法?

谢谢

4

2 回答 2

1

我的猜测——这只是一个猜测——是其中一种方法FindClassGetStaticMethodId由于某种原因没有成功,并且由于您没有检查错误,因此您的程序最终会崩溃,试图通过一个坏的方法调用一个方法方法指针。最低限度地检查这两种方法的返回值,NULL如果这是它们返回的值,则失败。

在@TimBender 的敦促下,我再次查看了您的代码,我同意——它可能是FindClass. 你绝对不能~在你的classpath-- 只有 shell 知道如何解释它,并且该参数在 JVM 看到它之前不会通过 shell。使用指向类位置的完整绝对路径——实际上是包层次结构的根。

于 2013-06-07T23:06:56.123 回答
1

根据有限的研究,我 99% 确定问题出在FindClass声明上。FindClass接受一个完全限定的类名作为第二个参数。完全限定意味着包名必须包含在类名之前。我假设它javaTest不在默认包中,或者如果它仍然存在一些引用它的约定。

于 2013-06-07T23:20:31.307 回答