KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > common > BrowserLauncher


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin.common;
5
6 import java.lang.reflect.Method JavaDoc;
7 import javax.swing.JOptionPane JavaDoc;
8
9 public class BrowserLauncher {
10   private static final String JavaDoc errMsg = "Error attempting to launch web browser";
11   
12   private static final String JavaDoc[] UNIX_BROWSERS = {
13     "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"
14   };
15
16   public static void openURL(String JavaDoc url) {
17     String JavaDoc osName = System.getProperty("os.name");
18     
19     try {
20       if(osName.startsWith("Mac OS")) {
21         Class JavaDoc fileMgr = Class.forName("com.apple.eio.FileManager");
22         Method JavaDoc openURL = fileMgr.getDeclaredMethod("openURL", new Class JavaDoc[] {String JavaDoc.class});
23         
24         openURL.invoke(null, new Object JavaDoc[] {url});
25       }
26       else if(osName.startsWith("Windows")) {
27         Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
28       }
29       else {
30         String JavaDoc browser = null;
31         
32         for(int count = 0; count < UNIX_BROWSERS.length && browser == null; count++) {
33           if(Runtime.getRuntime().exec(new String JavaDoc[] {"which", UNIX_BROWSERS[count]}).waitFor() == 0) {
34             browser = UNIX_BROWSERS[count];
35           }
36         }
37         
38         if(browser == null) {
39           throw new Exception JavaDoc("Could not find web browser");
40         }
41         else {
42           Runtime.getRuntime().exec(new String JavaDoc[] {browser, url});
43         }
44       }
45     } catch(Exception JavaDoc e) {
46       JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
47     }
48   }
49 }
50
Popular Tags