2

I'm experiencing a very weird behavior from JavaFX. It is related to Why are Runtime.getRuntime().exec and ProcessBuilder starting my second jar application after the first one exits? that wasn't exactly answered. The proposed workaround is sadly not applicable to my needs.

The behavior arises when starting a process either through ProcessBuilder or Runtime.exec() from a JavaFX application specifically. Here's a test code:

package testingfx;

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TestingFX extends Application {       

   @Override
   public void start(Stage stage) {
      StackPane root = new StackPane();
      Scene scene = new Scene(root, 300, 250);
      stage.setScene(scene);
      stage.show();

      ProcessBuilder builder; 
      builder = new ProcessBuilder(
            "C:\\Program Files\\MPC-HC\\mpc-hc64.exe");
      try {
         builder.start();
      } catch (IOException x) {
         System.err.println(x);
      }
   }

   public static void main(String[] args) {
      launch(args);
   }

}

If you happen to have MPC-HC installed on your computer you will see it being executed as soon as the application starts, as expected. However, if I now replace the parameter with "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", VLC will only execute when the application is closed.

I was unable to find any information on why this is happening. The same code in Swing is working as expected, so what give? Ideas are most welcome.

Xunkar
  • 115
  • 1
  • 10
  • 1
    See if using `builder.inheritIO().start()` instead of `builder.start()` works. If that works, then VLC might be printing things to its output and cannot continue because no one is reading (i.e. emptying) the buffer; I have no idea how exiting the parent process affects situations like that. – Slaw Sep 26 '18 at 13:53
  • Indeed, it works. Specifically VLC was writing a lot of text in its error stream. I was under the impression that such redirection was not necessary, thank you for your help. (Please do post this as a reply so I can properly validate your answer) – Xunkar Sep 26 '18 at 14:07
  • 1
    Related: [Why does process hang if the parent does not consume stdout/stderr in Java?](https://stackoverflow.com/questions/16983372/why-does-process-hang-if-the-parent-does-not-consume-stdout-stderr-in-java?noredirect=1&lq=1). Also, note you don't have to use `inheritIO`; you could use threads to read the out/err streams or redirect them to a file. – Slaw Sep 26 '18 at 14:22

0 Answers0