打开不在Mac OS上运行的浏览器链接

时间:2013-04-11 13:45:10

标签: java

点击按钮,我希望应用程序打开一个URL。 所以我这样做:

Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
    System.out.println("Hey "+desktop);
    if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
        try {
            desktop.browse(new URL("http://support.apple.com/kb/DL1572").toURI());
            System.out.println("here");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

但是Desktop.isDesktopSupported()返回false。我在Mac OS X 10.7.5上。还有其他选择吗?

1 个答案:

答案 0 :(得分:1)

在Mac上,这可以胜任。感谢this

private void openUrlInBrowser(String url)
{
 Runtime runtime = Runtime.getRuntime();
 String[] args = { "osascript", "-e", "open location \"" + url + "\"" };
 try
 {
  Process process = runtime.exec(args);
 }
 catch (IOException e)
 {
// do what you want with this
 }
}
相关问题