Java:打开文件(Windows + Mac)

7

可能重复的问题:
如何从Java启动给定文件的默认(本机)应用程序?

我有一个打开文件的java应用程序。在Windows上完美运行,但在Mac上不行。

问题在于我使用了Windows配置来打开它。代码如下:

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);

现在我的问题是,在mac中如何打开它?或者是否有另一种跨平台打开PDF的方法?

编辑:

我按以下方式创建了文件:

File folder = new File("./files");
File[] listOfFiles = folder.listFiles();

在循环中,我将它们添加到一个数组中:

fileArray.add(listOfFiles[i]);

如果我尝试使用Desktop.getDesktop().open(file)从该数组打开文件,它会显示找不到该文件(路径混乱因为我使用 './files'作为文件夹)。
3个回答

16

这是一个操作系统检测器:

public class OSDetector
{
    private static boolean isWindows = false;
    private static boolean isLinux = false;
    private static boolean isMac = false;

    static
    {
        String os = System.getProperty("os.name").toLowerCase();
        isWindows = os.contains("win");
        isLinux = os.contains("nux") || os.contains("nix");
        isMac = os.contains("mac");
    }

    public static boolean isWindows() { return isWindows; }
    public static boolean isLinux() { return isLinux; }
    public static boolean isMac() { return isMac; };

}

然后你可以像这样打开文件:

public static boolean open(File file)
{
    try
    {
        if (OSDetector.isWindows())
        {
            Runtime.getRuntime().exec(new String[]
            {"rundll32", "url.dll,FileProtocolHandler",
             file.getAbsolutePath()});
            return true;
        } else if (OSDetector.isLinux() || OSDetector.isMac())
        {
            Runtime.getRuntime().exec(new String[]{"/usr/bin/open",
                                                   file.getAbsolutePath()});
            return true;
        } else
        {
            // Unknown OS, try with desktop
            if (Desktop.isDesktopSupported())
            {
                Desktop.getDesktop().open(file);
                return true;
            }
            else
            {
                return false;
            }
        }
    } catch (Exception e)
    {
        e.printStackTrace(System.err);
        return false;
    }
}

对您的编辑进行回答:

尝试使用 file.getAbsoluteFile() 或者甚至是 file.getCanonicalFile()


1
为什么你在Linux/OS X上使用String[]变量的exec,但在Windows上不使用? - Mot
马丁,我刚在我的旧 Mac 电脑上测试了一下,但它无法运行。它显示“UnsupportedClassVersion”。我应该使用其他工具吗? - Tjekkles
我在操作系统检测器中更改了 Mac 的条件,请再试一次。 - Martijn Courteaux
具有讽刺意味的是,toLowercase() 应该是 toLowerCase(),但它不允许我编辑您的帖子。此外,对于 Windows,您应该将 rundll 和其余部分分开,如下所示:"rundll32", "url.dll,FileProtocolHandler",因为您编写的方式会产生错误,因为没有 "rundll32 url.dll,FileProtocolHandler.exe" - Dreen

15

起初,任何与*.dll相关的都是Windows风格的。

也许你可以尝试下面的代码在Linux上运行,它可能也能在MAC上工作:

import java.awt.Desktop;
import java.io.File;

Desktop d = Desktop.getDesktop();  
d.open(new File("foo.pdf"))

1
我在 Mac OS X 上工作。 - Martijn Courteaux
当你声明d时,它是什么类型的对象? - Tjekkles
1
@Mi Mee:由于他不知道d是什么,我稍微编辑了一下你的答案,以帮助OP。 - Martijn Courteaux
1
还要调用Desktop.isDesktopSupported(),以确保安全。请记住,这仅适用于Java 6。 - Denis Tulskiy
1
谢谢!在我的Mac和Windows 10上都有效。 - Hermandroid
显示剩余4条评论

3
你需要查看命令open,因此请执行。
Runtime.getRuntime().exec("/usr/bin/open " + file);

由Martijn编辑
当您在文件路径中使用空格时,这样会更好:

Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()});

当我运行这个程序时,它给出了一个 IOException 错误:“Cannot run program "open": Can't find given file”。然而,在我的问题中提到的代码却完全正常运行。 - Tjekkles
1
尝试使用open的完整路径。 - mmmmmm
我怎样才能知道“open”的完整路径呢?它必须在多个平台上运行,因为“open”在每个操作系统上的位置都不同。 - Tjekkles
你得到了我的加一,它在Mac OS X上运行良好。 - Martijn Courteaux
有没有可能检查系统是Mac还是Windows?我目前正在Windows上测试它,因为它应该在两个平台上都能工作。 - Tjekkles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接