Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8195086

Using the Desktop.open() method from a JavaFX application hangs the application. It works from a Java application

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_151"

      ADDITIONAL OS VERSION INFORMATION :
      Linux XPS-15-9560 4.13.0-26-generic #29~16.04.2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Using Desktop.getDesktop().open(f); to open a .pdf file works in a Java application, but in a JavaFX application it hangs the application and does not open. This happens on Ubuntu Linux, but not on windows using the same .pdf file. On Windows 10 the .pdf file is opened with the default .pdf viewer.

      On Ubuntu when I double click the .pdf file from a File Manager it opens using Document Viewer, the registered application. From the command line if I do "gnome-open example.pdf" the file opens in Document Viewer so the Document Viewer is registered properly with the OS to open .pdf files.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the JavaFX app below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      .pdf file should open in the default .pdf viewer.
      ACTUAL -
      JavaFX app hangs.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class FxDesktopExample extends Application {

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

      @Override
      public void start(Stage primaryStage) throws Exception {

      HBox hbox = new HBox();

      // Label l = new Label("Test Desktop.getDesktop.open()");
      // hbox.getChildren().add(l);
      Button b = new Button("Test Desktop.getDesktop.open()");
      hbox.getChildren().add(b);

      b.setOnAction(new EventHandler<ActionEvent>() {

      public void handle(ActionEvent event) {
      // TODO Auto-generated method stub

      String filename = "example.pdf";
      File f = new File(filename);

      if (f.exists()) {

      System.out.println(f.getAbsolutePath() + " exists.");
      if (Desktop.isDesktopSupported()) {
      try {
      Desktop.getDesktop().open(f);
      } catch (IOException e) {
      System.out.println("Could not open " + f.getName());
      }
      } else {
      System.out.println("Desktop not supported");
      }
      } else {
      System.out.println(f.getAbsolutePath() + " does not exist");
      }
      }

      });

      Scene scene = new Scene(hbox);
      primaryStage.setScene(scene);
      primaryStage.setTitle("Desktop example");
      primaryStage.show();


      // String filename = "example.pdf";
      // File f = new File(filename);
      //
      // if (f.exists()) {
      //
      // System.out.println(f.getAbsolutePath() + " exists.");
      // if (Desktop.isDesktopSupported()) {
      // try {
      // Desktop.getDesktop().open(f);
      // } catch (IOException e) {
      // System.out.println("Could not open " + f.getName());
      // }
      // } else {
      // System.out.println("Desktop not supported");
      // }
      // } else {
      // System.out.println(f.getAbsolutePath() + " does not exist");
      // }

      }
      }

      public class DesktopExampleMain {

      public static void main(String[] args) {

      String filename = "example.pdf";
      File f = new File(filename);

      if (f.exists()) {
      if (Desktop.isDesktopSupported()) {

      try {
      Desktop.getDesktop().open(f);
      } catch (IOException e) {
      System.out.println("Could not open " + f.getName());
      }
      } else {
      System.out.println("Desktop not supported");
      }
      } else {
      System.out.println(f.getAbsolutePath() + " does not exist");
      }

      }
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Instead of using the Desktop class use this code:

      String filename = "example.pdf";
      File f = new File(filename);

      try {
      Runtime.getRuntime().exec("gnome-open " +f);
      } catch (IOException e) {
       logger.error("gnome-open failed to open " + downloadedFile.getAbsolutePath(), e);
      }


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bvaidya Balchandra Vaidya
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: