Ossama
Ossama

Reputation: 2433

Using Java processbuilder to turn of HDMI tv

I would like to send the following command below from a java program, but not overly bothered about reading the response. any idea how I can do this

the command below turns of the TV through CEC cammand

echo "standby 0000" | cec-client -d 1 -s "standby 0" RPI

I am lloking at something like the following code below, but not sure how I can fit the above comand to it

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

Upvotes: 1

Views: 282

Answers (2)

Reimeus
Reimeus

Reputation: 159754

Try this

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

The pipe operator | is interpreted by the command shell so bash is used

Upvotes: 2

How about something like this:

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

Upvotes: 1

Related Questions