有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

桌面上的JavaFX冻结。打开(文件),桌面。浏览(uri)

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

当在Main中或在其他空的Java项目中调用时,以下函数可以完美地工作,但是当从任何JavaFX应用程序调用时,它会导致窗口冻结并停止响应(尽管项目完全符合要求),需要强制关闭它

有人能建议我所写的可能导致问题或循环的任何问题吗

唉,由于故障模式,我无法提供或分析错误消息

感谢您的建议,提前感谢

   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);
            }
}

共 (5) 个答案

  1. # 1 楼答案

    将其封装在系统线程上:

        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());
    
  2. # 2 楼答案

    我解决了…的问题

     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();
            }
        }
    
    }
    
  3. # 3 楼答案

    在JavaFX中有一种新的处理方法。我看到的唯一缺点是需要使用Application单例实例化HostServicesDelegate

    HostServicesDelegate hostServices = HostServicesFactory.getInstance(appInstance);
    hostServices.showDocument("http://www.google.com");
    
  4. # 4 楼答案

    我也有同样的问题,这个解决方案对我有效:

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

    我也有同样的问题。我发现如果我打电话给桌面。从一个新线程打开()方法,在关闭JavaFX应用程序窗口后,文件将打开,但这没有多大帮助

    如果你把

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

    在您的main方法启动后(args)调用中,在您关闭JavaFX应用程序之前,它也不会被调用

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

    在Ubuntu上你可以试试

    xdg-open filename
    

    从您的JavaFX应用程序

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