Sarah Szabo
Sarah Szabo

Reputation: 10815

Strange Exception In JavaFX8 When System.Exit Is Called from Start Method

If I have a JavaFX application with this start method, I get an error saying that the stream couldn't find the file to write, or that it doesn't exist, which is curious since the constructor on the PrintStream class says that it will create the file. But, when the System.exit statement is removed, is works normally. What is happening?

`@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
    System.setOut(new PrintStream(Paths.get("Resources",
            System.nanoTime() + ".dat").toFile()));
    System.out.println("Hello World!");
    System.exit(0);
}`

Exception: Caused by: java.io.FileNotFoundException: Resources\Error Logs\86138494710083.dat (The system cannot find the path specified)

Upvotes: 0

Views: 85

Answers (1)

jewelsea
jewelsea

Reputation: 159376

To exit a JavaFX application, you should call Platform.exit(), not System.exit().

I do not know what your specific FileNotFoundException exception pertains to. However, I believe it is best to shutdown the JavaFX platform cleanly so that the Application lifecycle is completed. I think that if you do not shut down the platform cleanly, you can expect unpredictable behaviour.

Upvotes: 1

Related Questions