使用Java processbuilder打开HDMI电视

使用Java processbuilder打开HDMI电视,java,processbuilder,Java,Processbuilder,我想从一个java程序发送下面的命令,但不想过分担心读取响应。你知道我该怎么做吗 下面的命令通过CEC cammand打开电视 echo "standby 0000" | cec-client -d 1 -s "standby 0" RPI 我正在查找下面类似的代码,但不确定如何将上面的代码与之匹配 ProcessBuilder builder = new ProcessBuilder("ls", "-l"); // or whatever your command is builder.re

我想从一个java程序发送下面的命令,但不想过分担心读取响应。你知道我该怎么做吗

下面的命令通过CEC cammand打开电视

echo "standby 0000" | cec-client -d 1 -s "standby 0" RPI
我正在查找下面类似的代码,但不确定如何将上面的代码与之匹配

ProcessBuilder builder = new ProcessBuilder("ls", "-l"); // or whatever your command is
builder.redirectErrorStream(true);
Process proc = builder.start();
试试这个

ProcessBuilder processBuilder = 
  new ProcessBuilder("bash", "-c", "echo \"standby 0000\" | cec-client -d 1 -s \"standby 0\" RPI");
Process process = processBuilder.start();

管道操作符
|
由命令shell解释,因此使用
bash

类似的内容如何:

import java.io.*;

public class SendCommandToTV {

    public static void main(String args[]) {

        String s = null;

        try {

        Process p = Runtime.getRuntime().exec("echo \"standby 0000\" | cec-client -d 1 -s \"standby 0\" RPI");

            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new 
                 InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

            System.exit(0);
        }
        catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

现在看起来很好,抱歉,我正在测试:)您是否显示了
ErrorStream
的输出?抱歉,请问我该怎么做?为什么?我们必须读回缓冲区才能执行它吗。一旦命令被执行,它甚至没有显示任何内容。哈哈哈,我没有启动process=processBuilder.start();