Java Desktop.open() 永远不会完成

huangapple 未分类评论51阅读模式
英文:

Java Desktop.open() never finish

问题

以下是翻译好的内容:

这里有一个永远不会结束的代码示例。我所做的就是使用 Desktop.open(file) 打开一个文件。在我关闭打开的文件之后,程序就会关闭(例如弹出的文本编辑器或 Excel 文件)。

public static void main(String[] args) {
    File file = new File("TextFile1.log");
    if (file.exists()) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Desktop desktop = Desktop.getDesktop();
                System.out.println("使用桌面协议打开 " + desktop.toString());
                try {
                    // Runtime.getRuntime().exec("gedit " + file); // 这行代码可以正常工作,但与 Linux 相关
                    desktop.browse(file.toURI());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.out.println("已打开文件 " + file.getName());
            }
        };
        thread.start();
    } else {
        System.out.println("文件不存在!");
    }
    try {
        Thread.sleep(1000L); // 为了确保在文件实际打开之前不会退出
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("程序结束。");
    System.exit(0);
}

我得到的输出如下:

使用桌面协议打开 java.awt.Desktop@7a8afb69
已打开文件 TextFile1.log
程序结束。

过了一段时间,我决定关闭已打开的文件,输出中还会出现以下内容:

进程以退出代码 0 结束

如何在不阻止退出且支持跨平台的情况下打开文件呢?我在 Windows 上也注意到了这一点。全部基于 Java 8。

英文:

Here is a code example that will never finish. All I do is open a file with Desktop.open(file). Program closes after I close opened file (for example text editor that pops up, or excel file).

public static void main(String[] args) {
    File file = new File("TextFile1.log");
    if (file.exists()) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Desktop desktop = Desktop.getDesktop();
                System.out.println("opening using desktop protocol " + desktop.toString());
                try {
//                  Runtime.getRuntime().exec("gedit "+file);//this line of code works fine, but it is linux related
                    desktop.browse(file.toURI());//
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                System.out.println("File opened " + file.getName());
            }
        };
        thread.start();
    } else {
        System.out.println("File does not exists!");
    }
    try {
        Thread.sleep(1000L);//just so we do not exit before file is actually opened
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("End of program.");
    System.exit(0);
}

Output that I get:

opening using desktop protocol java.awt.Desktop@7a8afb69
File opened TextFile1.log
End of program.

After some time, I decide to close opened file and I get below line in output as well:

Process finished with exit code 0

How can I open file without blocking exit and that cross platform is supported?

I noticed this on Windows as well. All on Java 8.

huangapple
  • 本文由 发表于 2020年5月29日 19:27:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/62084889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定