点击按钮,我希望应用程序打开一个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上。还有其他选择吗?
答案 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
}
}