J2SE 6 新增了 java.awt.Desktop ,這套桌面 API 使用你的主機操作系統的文件關聯以啟動與特定文件類型相關聯的應用程序。調用本地瀏覽器非常方便,且跨平臺適用。
           1public static void runBroswer(String webSite) {   
           2    try {   
           3        Desktop desktop = Desktop.getDesktop();   
           4        if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)){   
           5            URI uri = new URI(webSite);   
           6            desktop.browse(uri);   
           7        }
             
           8    }
           catch (IOException ex) {   
           9        ex.printStackTrace();   
          10    }
           catch (URISyntaxException ex) {   
          11        ex.printStackTrace();   
          12    }
             
          13}

          J2SE 5及之前可使用以下代碼

           1public static void openURL(String url) {
           2    String osName = System.getProperty("os.name");
           3    try {
           4        if (osName.startsWith("Mac")) {//Mac OS
           5            Class fileMgr = Class.forName("com.apple.eio.FileManager");
           6            Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[]{String.class});
           7            openURL.invoke(nullnew Object[]{url});
           8         }
           else if (
           9             osName.startsWith("Windows")){//Windows
          10             Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
          11         }
           else {   //Unix   or   Linux
          12             String[] browsers = {"firefox""opera""konqueror",
          13                    "epiphany""mozilla""netscape"}
          ;
          14             String browser = null;
          15             for (int count = 0; count < browsers.length && browser == null; count++{
          16                 if (Runtime.getRuntime().exec(
          17new String[]{"which", browsers[count]}).waitFor() == 0{
          18                     browser = browsers[count];
          19                 }

          20             }

          21             if (browser == null{
          22                 throw new Exception("Could not find web browser");
          23             }
           else {
          24                 Runtime.getRuntime().exec(new String[]{browser, url});
          25             }

          26        }

          27     }
           catch (Exception ex) {
          28        ex.printStackTrace();
          29     }

          30}

          31

          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/casularm/archive/2008/11/28/3401018.aspx
          posted on 2010-01-06 17:02 Bom Wu 閱讀(576) 評論(0)  編輯  收藏 所屬分類: java program

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          主站蜘蛛池模板: 河津市| 玉环县| 广河县| 宁津县| 杭锦旗| 财经| 美姑县| 汕头市| 延安市| 吉木乃县| 林芝县| 汝南县| 漾濞| 扎赉特旗| 柘荣县| 晋城| 慈利县| 喀什市| 精河县| 沁水县| 鹤岗市| 普兰店市| 渑池县| 张家界市| 景宁| 柞水县| 天全县| 九龙坡区| 尚义县| 泾阳县| 繁峙县| 历史| 五华县| 洪洞县| 聊城市| 长海县| 新宁县| 东乡| 庆安县| 天峨县| 务川|