gpt4 book ai didi

Java native 接口(interface) GetStringUTFChars fatal error

转载 作者:太空狗 更新时间:2023-10-29 11:45:02 27 4
gpt4 key购买 nike

我有一个名为“Robotic Arm Edge”的机器人和 USB 接口(interface)。我正在尝试制作自定义软件,但这涉及到 Java native 接口(interface)。我选择 Java 是因为它比用于 C/C++ 的 ATK+ 库更容易制作 gui 界面。我需要 Java Native 接口(interface)的原因是 libusb,我已经有一个我自己编写的工作 Controller 。现在,我需要做的就是编写一个 Java 包装器,但我需要将 jstring 转换为 c 字符串。 GetStringUTFChars JNI 方法总是导致 fatal error ,我已经尝试了几乎所有可能的方法变体。这是 Java 类:

RoboController.java:

package arm.robot;

import arm.robot.enums.*;


public class RoboController {


static {
System.loadLibrary("robojava");
sendToRobo0("");
}

static Pinchers m1 = Pinchers.stopped;
static Joint m2 = Joint.stopped;
static Joint m3 = Joint.stopped;
static Joint m4 = Joint.stopped;
static Base m5 = Base.stopped;
static Light l = Light.off;

private static native boolean sendToRobo0(String cmds);

public static void sendToRobo(){
StringBuilder sb = new StringBuilder("");
boolean first = true;

if(m1.equals(Pinchers.opening)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m1o");
}
else if(m1.equals(Pinchers.closing)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m1c");
}

if(m2.equals(Joint.up)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m2u");
}
else if(m2.equals(Joint.down)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m2d");
}

if(m3.equals(Joint.up)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m3u");
}
else if(m3.equals(Joint.down)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m3d");
}

if(m4.equals(Joint.up)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m4u");
}
else if(m4.equals(Joint.down)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m4d");
}

if(m5.equals(Base.clockwise)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m5cw");
}
else if(m5.equals(Base.counterclockwise)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("m5ccw");
}

if(l.equals(Light.on)) {
if(!first){
sb.append(" ");
}
else first = false;
sb.append("lon");
}
sendToRobo0(sb.toString());
}
}

来自 javah 的 header :

arm_robot_RoboController.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class arm_robot_RoboController */

#ifndef _Included_arm_robot_RoboController
#define _Included_arm_robot_RoboController
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: arm_robot_RoboController
* Method: sendToRobo0
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_arm_robot_RoboController_sendToRobo0
(JNIEnv *, jclass, jstring);

#ifdef __cplusplus
}
#endif
#endif

最后是发生 fatal error 的实现:

arm_robot_RoboController.cpp

#include <jni.h>
#include <string>
#include <iostream>
#include "arm_robot_RoboController.h"

using namespace std;


JNIEXPORT jboolean JNICALL Java_arm_robot_RoboController_sendToRobo0(JNIEnv* env, jclass c, jstring args){
const char *str= env->GetStringUTFChars(args,(jboolean*)0); //FATAL ERROR HERE
//string s(str);
//cout << s;
return true;
}

错误日志:

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xb676a978, pid=2912, tid=2842113136
#
# JRE version: Java(TM) SE Runtime Environment (7.0_40-b43) (build 1.7.0_40-b43)
# Java VM: Java HotSpot(TM) Client VM (24.0-b56 mixed mode linux-arm )
# Problematic frame:
# V [libjvm.so+0x262978] jni_GetStringUTFChars+0x74
#
# Core dump written. Default location: /home/pi/robotic arm/gui/core or core.2912
#
# An error report file with more information is saved as:
# /home/pi/robotic arm/gui/hs_err_pid2912.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Aborted (core dumped)

我使用的构建命令行:

g++ -c -I"/usr/lib/jvm/jdk-7-oracle-armhf/include" -I"/usr/lib/jvm/jdk-7-oracle-armhf/include/linux" robo.cpp
g++ -I"/usr/lib/jvm/jdk-7-oracle-armhf/include" -I"/usr/lib/jvm/jdk-7-oracle-armhf/include/linux" -olibrobojava.so -shared -Wl,-soname,robojava.so arm_robot_RoboController.cpp robo.o

我使用的是运行接近最新 Raspbian Wheezy 的 Raspberry Pi Model B。我无法找出我做错了什么。

最佳答案

正如在 EJP 评论中一样,我之前的建议是由于缺少 &isCopy 而导致错误,我只留下 ReleaseStringUTFChars 部分。我只能建议在看起来可疑的调用之前和之后添加日志记录,你也应该查看 logcat,当 native 代码中的 jni 发生错误时,android 有时会在那里输出提示。


你还必须调用:

ReleaseStringUTFChars

使用str后

关于Java native 接口(interface) GetStringUTFChars fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com