使用Java processbuilder打开HDMI电视

时间:2013-09-19 20:41:50

标签: 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.redirectErrorStream(true);
Process proc = builder.start();

2 个答案:

答案 0 :(得分:2)

试试这个

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

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

答案 1 :(得分:1)

这样的事情怎么样:

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);
        }
    }
}