Java 如何在桌面上用特定程序打开文件

Java 如何在桌面上用特定程序打开文件,java,Java,我正在使用java.awt.Desktop打开一个文件。我无法控制文件类型程序关联,我想打开一个带有非默认特定程序的文件。我该怎么做 这是我的代码: public static void open(File document) throws IOException { Desktop dt = Desktop.getDesktop(); dt.open(document); } 我认为桌面类不支持这一点;没有用于查询文件关联的API 是否希望用户从列表

我正在使用java.awt.Desktop打开一个文件。我无法控制文件类型程序关联,我想打开一个带有非默认特定程序的文件。我该怎么做

这是我的代码:

 public static void open(File document) throws IOException {
        Desktop dt = Desktop.getDesktop();
        dt.open(document);
    }

我认为桌面类不支持这一点;没有用于查询文件关联的API

是否希望用户从列表中进行选择?如果是这样的话,我认为你必须按照平台来做。对于Windows,您可以参考以下问题:

或者你已经有了一个具体的计划?如果是,则可以直接使用Runtime.exec启动它:

如果知道要执行的程序的名称和位置,可以直接运行该程序,并在空格后提供文件名作为参数打开:

try {
    Runtime runTime = Runtime.getRuntime();
    // Don't forget that '\' needs to be escaped with another '\'
    // Also, there may be spaces in the name(s). Use quotes (with their own escapes!)
    Process process = runTime.exec("\"C:\\Windows\\system32\\notepad.exe\"" +
                                   " " +    // Separate argument with space
                                   "\""+document.getAbsolutePath()+"\"");
} // try
catch (IOException e) {
    e.printStackTrace();
} // catch

为什么投反对票。。。?