How can launch an external process from java and still be able to interact with this process?

227 views Asked by At

My application should launch an external program to start recording the desktop. I am using a simple program: recordmydesktop. Launching the program works fine using ProcessBuilder.

My main issue is that I have to stop the recording. But I don't have access to the program anymore.

My first idea was to launch a terminal from java: bash did not stay open but xterm worked. First of all I would like to know why bash shell stay opened?

Then, I would like to find: How can I not use the xterm and still being able to stop the recording process? For example: send stop signal (Ctrl C) to the process.

Here is some sample code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class hh {

public static void main(String[] args) {
    try {
        Process process = new ProcessBuilder(new String[]{"/usr/bin/xterm" ,"recordmydesktop"}).start();
        InputStream processIS = process.getInputStream();
        InputStreamReader processISR = new InputStreamReader(processIS);
        BufferedReader processBR = new BufferedReader(processISR);
        String line;
        System.out.println("Output of the record process is: ");

        while ((line=processBR.readLine())!=null){
            System.out.print(line);
        }
        }
    catch (IOException e) {
        e.printStackTrace();
    }
}
}

Also, in this code, I am not able to get any line back. So my program does not know if the recordmydesktop is ok.

Ps: If you launch:

Process process = new ProcessBuilder(new String[]{"/usr/bin/xterm" ,"xterm"}).start();

Many xterm open instead of two. I create a loop that should not happen. This is not linked to my problem but if someone know the reason I am curious to know why.

Thanks for your help !

1

There are 1 answers

1
dani1709 On

You can send a command to the process via the Output stream.

Process process ;
String command = "some command" ;
process.getOutputStream().writeBytes( command.getBytes() ) ;