我在Java中有一个函数正在树莓派上执行,并且应该发送信号将目标套接字状态切换为开/关。
那就是我当前的功能:
public static void rcswitch(int housecode,int unitcode, int onoff) throws InterruptedException, IOException {
String housestring = Integer.toString(housecode);
String unitstring = Integer.toString(unitcode);
String onoffstring = Integer.toString(onoff);
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "sudo /home/pi/rcswitch-pi/send", housestring, unitstring, onoffstring);
Process proc = builder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
}
但是,终端似乎不接收命令,因为它不输出任何内容。它应该显示类似“收到命令”的内容,然后执行它。当我在终端中正常执行/ send命令时,它可以正常工作。在eclipse中,它可以正常工作并抛出预期的错误。
感谢您的回答:)
最佳答案
执行命令时最有可能发生错误。请记住,Process#getInputStream()不包括该过程的标准错误流。您应该使用Process#getErrorStream()。就像是:
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}