JavaFX在Desktop.open(文件),Desktop.browse(uri)上冻结

我正在使用Oracle JDK 1.8.0_05通过NetBeans8.0在Ubuntu 12.04 LTS 64位(使用Gnome Shell)上运行一些Java代码。

当在Main或其他空的Java项目中调用以下函数时,如果在任何JavaFX应用程序中调用此函数,都会导致窗口冻结并停止响应(尽pipe项目完全符合),但要求它是“强制closures”。

任何人都可以提出任何问题,我写了哪些可能导致问题或循环?

唉,由于失败的模式,没有错误信息,我可以提供或分析。

任何build议感激地收到,在此先感谢。

public static void desktopTest(){ Desktop de = Desktop.getDesktop(); try { de.browse(new URI("http://stackoverflow.com")); } catch (IOException | URISyntaxException e) { System.out.println(e); } try { de.open(new File("/home/aaa/file.ext")); } catch (IOException e){ System.out.println(e); } try { de.mail(new URI("mailto:email@example.com")); } catch (URISyntaxException | IOException e){ System.out.println(e); } } 

我也有同样的问题,这个解决方案适用于我:

 if( Desktop.isDesktopSupported() ) { new Thread(() -> { try { Desktop.getDesktop().browse( new URI( "http://..." ) ); } catch (IOException | URISyntaxException e1) { e1.printStackTrace(); } }).start(); } 

我解决了问题

  public static void abrirArquivo(File arquivo) { if (arquivo != null) { if (arquivo.exists()) { OpenFile openFile = new OpenFile(arquivo); Thread threadOpenFile = new Thread(openFile); threadOpenFile.start(); } } } private static class OpenFile implements Runnable { private File arquivo; public OpenFile(File arquivo) { this.arquivo = arquivo; } private void abrirArquivo(File arquivo) throws IOException { if (arquivo != null) { java.awt.Desktop.getDesktop().open(arquivo); } } @Override public void run() { // TODO Auto-generated method stub try { abrirArquivo(arquivo); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

我也有这个相同的问题。 我发现,如果我从一个新线程调用Desktop.open()方法,那么关闭JavaFX应用程序窗口之后 ,该文件将会打开,但这并没有什么帮助。

如果你放

 SwingUtilities.invokeLater(() -> System.out.println("Hello world")); 

在你的启动(args)调用之后,你的主要方法在关闭JavaFX应用程序之前也不会被调用。

JavaFX应用程序和Swing之间似乎存在某种并发问题。

在Ubuntu上,你可以尝试

 xdg-open filename 

从你的JavaFX应用程序。

据我所知,你的代码应该工作。

JavaFX中有一个新的方法来处理这个问题。 我看到唯一的缺点是你需要使用Application单例实例化一个HostServicesDelegate

 HostServicesDelegate hostServices = HostServicesFactory.getInstance(appInstance); hostServices.showDocument("http://www.google.com"); 

封装在系统线程上:

  final String url = "www.google.com"; final Hyperlink hyperlink = new Hyperlink("Click me"); hyperlink.setOnAction(event -> new Thread(() -> { try { Desktop.getDesktop().browse(new URI(url)); } catch (IOException | URISyntaxException e1) { e1.printStackTrace(); } }).start());