There is a thread that starts a process. Then you need to complete the stream along with the process. How to do it?

Stream class code:

package threads; import java.io.BufferedReader; import java.io.InputStreamReader; public class MyRunnable implements Runnable { private int var; public volatile boolean shutdown; public MyRunnable(int var) { this.var = var; } @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { String cmd = "gedit"; Process proc = Runtime.getRuntime().exec(cmd); System.out.println("Command executed"); String line; try (BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()))) { while ((line = input.readLine()) != null) { System.out.println(line); } } BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); String s = null; // 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); } } } catch (Exception e) { System.err.println("gedit error : " + e.getLocalizedMessage()); } } } 

Actually the main method:

 package threads; public class Threads { /** * @param args the command line arguments * @throws java.lang.InterruptedException */ public static void main(String[] args) throws InterruptedException { MyRunnable myRunnable = new MyRunnable(0); Thread t = new Thread(myRunnable); System.out.println("t.getState 1 : " + t.getState()); t.start(); Thread.sleep(2000); t.interrupt(); System.out.println("t.getState 2 : " + t.getState()); } } 

Output:

run: t.getState 1: NEW
Command executed
t.getState 2: RUNNABLE

Well, gedit itself is also safely executed.

What is the correct method to stop the stream with the gedit process?

  • Please explain in words the problem, not the solution. - Mikhail Vaysman

1 answer 1

Slightly changed your code, it turned out the following:

 public class Executor { private Process process; private final String command; Executor(String command) { this.command = command; } public void run() throws Exception { process = Runtime.getRuntime().exec(command); System.out.println("Command executed"); Thread runner = new Thread(() -> { try { try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while (process.isAlive() && (line = input.readLine()) != null) System.out.println(line); } //небольшая задержка, т.к. иногда не успевает проставиться флаг TimeUnit.MILLISECONDS.sleep(100); if (!process.isAlive()) { System.out.println("process " + command + " has been stopped!"); return; } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); try (BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { String errorCode; while (process.isAlive() && (errorCode = stdError.readLine()) != null) System.out.println(errorCode); } } catch (Exception e) { System.err.println(command + " error : " + e.getLocalizedMessage()); } }); runner.start(); } public void shutdown() { process.destroy(); } } 

Use as follows:

 Executor process = new Executor("pluma"); //запуск process.run(); //остановка process.shutdown();