我正在尝试运行一个简单的c代码,它调用一个jar文件,并在其中运行3个方法(这些函数调用C++共享对象。jar已经用.so编译,并且在通过java程序使用时运行时没有错误。
我现在得到一份工作 Segmentation fault(core dumped)
尝试运行c文件时出错。编译运行时没有任何错误。
汇编行:
gcc -o run java_calls.c -I/usr/lib/jvm/java-16-oracle/include/linux -I/usr/lib/jvm/java-16-oracle/include -L/usr/lib/jvm/java-8-openjdk-arm64/jre/lib/aarch64/server -ljvm
我尝试调试错误,并使用 -g
标记,然后运行 gdb run core
并得到以下错误:
Starting program: /home/user/Desktop/JavaFromC/run
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
Program received signal SIGSEGV, Segmentation fault.
0x0000007fb75072b4 in ?? () from /usr/lib/jvm/java-1.8.0-openjdk-arm64/jre/lib/aarch64/server/libjvm.so
c程序:
# include <jni.h>
# include <stdlib.h>
# include <stdio.h>
JNIEnv* create_jvm(JavaVM**java)
{
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
int res;
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
options.optionString = "-Djava.class.path=./JavaC.jar";
res = JNI_CreateJavaVM(java, (void**)&env, &vm_args);
if(res < 0 || !env) {
printf("Failed to create Java VM\n");
} else {
printf("JVM Created\n");
}
return env;
}
int main(int argc, char *argv[])
{
JavaVM *jvm;
JNIEnv *env;
jclass java_c;
jmethodID main_method;
jmethodID power_method;
jmethodID root_method;
env = create_jvm(&jvm);
if(env == NULL) {
exit(1);
}
java_cp = (*env)->FindClass(env, "JavaC");
if(java_cp == NULL) {
printf("Failed to find JavaC class\n");
return 1;
}
main_method = (*env)->GetStaticMethodID(env, java_c, "main", "([Ljava/lang/String;)V");
if(main_method == NULL) {
printf("Failed to find main function\n");
return 1;
}
power_method = (*env)->GetStaticMethodID(env, java_c, "powerMethod", "(I)I");
if(power_method == NULL) {
printf("Failed to find power function\n");
return 1;
}
root_method = (*env)->GetStaticMethodID(env, java_c, "rootMethod", "(D)D");
if(root_method == NULL) {
printf("Failed to find root function\n");
return 1;
}
jint a_num = 12;
jdouble b_num = 225;
(*env)->CallStaticVoidMethod(env, java_c, main_method, NULL);
printf("power = %d\n", (*env)->CallStaticIntMethod(env, java_c, power_method, a_num));
printf("root = %f\n", (*env)->CallStaticDoubleMethod(env, java_c, root_method, b_num));
return 0;
}
1条答案
按热度按时间neskvpey1#
这个答案可能与此相关。这个
SIGSEGV
在…内libjvm.so
这是“正常的”。您需要做的是继续执行程序(通过发送它)
SGISEGV
具有signal SIGSEGV
gdb命令)并让它处理信号,直到您收到SIGSEGV
这不是来自libjvm.so
.那你就知道真正的致命伤在哪里了
SIGSEGV
是从哪里来的,为什么。如果您仍然不理解问题的原因,则需要使用新观察到的信息(来自的输出)更新您的问题(编辑问题)info locals
,backtrace
等),可能会给出更好的答案。