import java.awt.Desktop;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;

//
// To run this program, create a temporary file named "test.txt" in the
// current directory. The program will open that file by calling
// java.awt.Desktop::open from the JavaFX Application Thread.
// This will deadlock on Linux.
//
public class FxSwingDeadlock extends Application {

    private Desktop desktop = Desktop.getDesktop();

    @Override
    public void start(final Stage stage) {

        // The following will cause a deadlock on Linux. If you wrap the
        // call to desktop.open in SwingUtilities::invokeLater it runs fine.
        final File file = new File("test.txt");
        System.err.println("Calling Desktop.open(" + file + ")");
        try {
            desktop.open(file);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        System.err.println("Desktop.open has finished");

        new Thread(() -> {
            // Exit after 5 seconds
            try { Thread.sleep(5000); } catch (InterruptedException ex) {}
            System.err.println("Calling Platform.exit");
            Platform.exit();
        }).start();
    }

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