英文:
JNI:converting jstring(full of number) into jint using Integer.pasteInt by jvmti caused Crash
问题
jclass in = jniEnv->FindClass("java/lang/Integer");
jmethodID ipi = jniEnv->GetStaticMethodID(in, "parseInt", "(Ljava/lang/String;)I");
jint test = jniEnv->CallStaticIntMethod(in, ipi, (jstring)jniEnv->CallStaticObjectMethod(System, getProperty, jniEnv->NewStringUTF("input")));
Here is my code
I try to conver a jstring("10029909473242") into a jint
But if i try to print it it crash the vm.
is there a better solution?
Thanks.
jclass JOptionPane = jniEnv->FindClass("javax/swing/JOptionPane");
jmethodID showInputDialog = jniEnv->GetStaticMethodID(JOptionPane, "showInputDialog", "(Ljava/lang/Object;)Ljava/lang/String;");
jniEnv->CallStaticObjectMethod(NULL, showInputDialog, test);
Please note that I've only provided the translated code portions, as you requested.
英文:
jclass in = jniEnv->FindClass("java/lang/Integer");
jmethodID ipi = jniEnv->GetStaticMethodID(in, "parseInt", "(Ljava/lang/String;)I");
jint test = jniEnv->CallStaticIntMethod(in, ipi, (jstring)jniEnv->CallStaticObjectMethod(System, getProperty, jniEnv->NewStringUTF("input")));
Here is my code
I try to conver a jstring("10029909473242") into a jint
But if i try to print it it crash the vm.
is there a better solution?
Thanks.
jclass JOptionPane = jniEnv->FindClass("javax/swing/JOptionPane");
jmethodID showInputDialog = jniEnv->GetStaticMethodID(JOptionPane, "showInputDialog", "(Ljava/lang/Object;)Ljava/lang/String;");
jniEnv->CallStaticObjectMethod(NULL, showInputDialog, test);
答案1
得分: 0
你没有显示实际错误,但我可以猜测一个原因:10029909473242太大了,无法适应Java的int
类型,所以我猜Integer.parseInt
抛出了一个NumberFormatException
。在有未决异常的情况下调用任何JNI方法都会导致JVM崩溃。
这里的根本原因是你应该使用Long.parseLong
,但你还应该在每次Call*Method
调用后通过调用ExceptionOccurred
或ExceptionCheck
来添加错误检查。然后要么返回到JVM,要么调用ExceptionClear
并在本机代码中继续。
英文:
You did not show an actual error, but I can guess at one reason:
10029909473242 is too large to fit into a Java int
so I bet Integer.parseInt
threw a NumberFormatException
. Calling any JNI method with a pending exception will crash the JVM.
The root cause here is that you should use Long.parseLong
instead, but you should additionally add error checking by calling ExceptionOccurred
or ExceptionCheck
after each Call*Method
call. Then either return to the JVM or call ExceptionClear
and continue in native code.
答案2
得分: 0
char buf1[64];
sprintf(buf1, "%d", p);
英文:
char buf1[64];
sprintf(buf1, "%d", p);
solved
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论