JNI代码中的FindClass导致JVM崩溃

3

我正在尝试从C++代码中编写一个相当基本的JNI调用。

JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[2];

// Get the default initialization arguments and set the class 
// path.
JNI_GetDefaultJavaVMInitArgs(&vm_args);
options[0].optionString = "-Djava.class.path=./hbase-1.0-SNAPSHOT.jar"
 options[1].optionString = "-verbose:jni";
vm_args.nOptions = 2;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;


// Load and initialize a Java VM, return a JNI interface 
// pointer in env.
long result = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (result == JNI_ERR) {
  LOG(ERROR) << "Failed to create a JVM";
  return false;
}

jclass cls = env->FindClass("com/scaligent/falcon/hbase/HFileJniReader");

我的代码在FindClass中崩溃。奇怪的是,我在类中有一个静态块,并且它打印了静态块中的消息。我在这里发布几行错误信息。 我无法弄清楚如何进行调试或解决此问题。

[Dynamic-linking native method java.lang.Package.getSystemPackage0 ... JNI]
[Dynamic-linking native method java.util.jar.JarFile.getMetaInfEntryNames ... JNI]
[Dynamic-linking native method java.lang.ClassLoader.defineClass1 ... JNI]
[Dynamic-linking native method java.io.FileOutputStream.writeBytes ... JNI]
Starting static block
[Dynamic-linking native method java.util.zip.Inflater.getBytesWritten ... JNI]
[Dynamic-linking native method sun.reflect.NativeMethodAccessorImpl.invoke0 ... JNI]
[Dynamic-linking native method java.security.AccessController.doPrivileged ... JNI]
[Dynamic-linking native method java.lang.Class.isAssignableFrom ... JNI]
[Dynamic-linking native method java.lang.System.identityHashCode ... JNI]
[Dynamic-linking native method java.util.zip.Inflater.end ... JNI]
[Dynamic-linking native method java.util.zip.ZipFile.close ... JNI]
[Dynamic-linking native method java.util.TimeZone.getSystemTimeZoneID ... JNI]
[Dynamic-linking native method sun.nio.fs.UnixNativeDispatcher.init ... JNI]
[Dynamic-linking native method sun.nio.fs.UnixNativeDispatcher.getcwd ... JNI]
[Dynamic-linking native method sun.nio.fs.UnixNativeDispatcher.realpath0 ... JNI]
[Dynamic-linking native method java.io.UnixFileSystem.getLength ... JNI]
[Dynamic-linking native method java.util.ResourceBundle.getClassContext ... JNI]
[Dynamic-linking native method sun.reflect.ConstantPool.getUTF8At0 ... JNI]
[Dynamic-linking native method java.lang.reflect.Proxy.defineClass0 ... JNI]
[Dynamic-linking native method java.lang.Class.isInstance ... JNI]
12/10/28 02:08:54 WARN conf.Configuration: fs.default.name is deprecated. Instead, use fs.defaultFS
 ending static block
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000000042f9e3, pid=7057, tid=140108980991808
#
# JRE version: 7.0_04-b20
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.0-b21 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [hfile_jni_reader_test+0x2f9e3]  ftell@@GLIBC_2.2.5+0x2f9e3
#
# Core dump written. Default location: /home/amit/git2/scaligent/falcon/hbase/core or core.7057
#
# An error report file with more information is saved as:
# /home/amit/git2/scaligent/falcon/hbase/hs_err_pid7057.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

似乎JNI会抛出通常它会捕获的信号,但在GDB中,它会导致GDB报告崩溃。我的代码还有其他错误,因为我依赖GDB进行调试而无法捕获。 - Amit Prakash
@amit:如果你的C++可执行文件使用了jar包中的其他第三方jar包或其他依赖项,则需要在Java代码库中指定它们的路径,根据你的规格说明,我只能找到这个问题。 - user1808932
是的,我确实依赖于其他 jar 包。但是我已经找到了问题并在这里添加了一条注释。 - Amit Prakash
3个回答

1

这可能是由于指针指向已经被删除的对象。请检查您的本地代码。


不确定您的意思。 在这种情况下,所有本机代码都在问题中。 我这里没有删除任何内容。 - Amit Prakash
删除意味着对象的内存已被释放。 - Techie Manoj
我明白 :) 我的意思是上面的本地代码没有释放任何东西。 - Amit Prakash

0

我后来意识到jni会抛出信号,通常情况下它会捕获,但在gdb中,它会导致gdb报告崩溃。我的代码有其他非常平凡的错误,因为我依赖gdb进行调试,所以无法捕获。


0

你可以告诉 GDB 忽略那些信号:

handle SIGSEGV nostop


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接