我在 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");
}
关于通过树莓派切换套接字状态的Java函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250952/