2013-04-11 114 views
0

點擊一個按鈕,我希望應用程序打開一個URL。 所以我這樣做:打開瀏覽器鏈接在Mac OS上不起作用

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

對我來說工作得很好。 OSX 10.7.5,build 11G63b。 – Perception 2013-04-11 13:49:16

+0

嗯,不在我的comp。它不可靠,將不得不尋找替代方案 – Jatin 2013-04-11 13:56:36

回答

0

在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 
} 
} 
相關問題