1

我想从 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();
4

2 回答 2

2

尝试这个

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

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

于 2013-09-19T20:45:21.373 回答
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);
        }
    }
}
于 2013-09-19T20:46:26.910 回答