如何在 windows [Java] 中获取扩展的默认应用程序

How to get default application for extension in windows [Java]

我正在尝试使用

Runtime.getRuntime().exec("Here_I_am.txt");

但是出现错误提示 Here_I_am.txt 不是 Win32 应用程序。

因此,我应该知道如果用户单击 .extension 文件将执行哪个程序。
默认情况下 txt 会是 Notepad.exe.
那么 - 我怎样才能得到这些信息?

P.S.
我确定此应用不会在 Linux.

使用

您必须通过命令shell:

调用它
Runtime.getRuntime().exec("cmd /c Here_I_am.txt");

您可以使用 Desktop.open(File)1。如果您需要在特定文件夹(如主目录)中获取,您还可以添加 System.getProperty("user.home")。像,

File file = new File(System.getProperty("user.home"), "Here_I_am.txt");
try {
    System.out.printf("File path %s%n", file.getCanonicalPath());   
    Desktop.getDesktop().open(file);
} catch (IOException e) {
    e.printStackTrace();
}

1它的小 优势 可以在 Windows、Linux 和 Mac。另见 How to Integrate with the Desktop Class.