JDK-8195086 : Using the Desktop.open() method from a JavaFX application hangs the application. It works from a Java application
  • Type: Bug
  • Component: javafx
  • Sub-Component: swing
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: linux
  • CPU: x86_64
  • Submitted: 2018-01-12
  • Updated: 2018-01-17
  • Resolved: 2018-01-17
Related Reports
Duplicate :  
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);
} 



Comments
The bug is not reproducible in JDK 9 or JDK 10. So, it will not be fixed in JDK 8 updates. Latest JDK ea builds are available at http://jdk.java.net/10/
17-01-2018

This issue is for Ubuntu only but is now fixed in JDK 9. Ubuntu 16.04.3 LTS ---------------------------- 8u151 : Fail 8u162 : Fail 9.0.1 : Pass 10-ea+35 : Pass
15-01-2018

Closing as duplicate of JDK-8096706
15-01-2018