KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > BrowserLauncher


1 package com.calipso.reportgenerator.common;
2
3 import java.lang.reflect.Method JavaDoc;
4
5 /**
6  *
7  * User: jbassino
8  * Date: 26-oct-2005
9  * Time: 15:24:27
10  *
11  */

12 public class BrowserLauncher {
13
14    private static final String JavaDoc errMsg = "Error attempting to launch web browser";
15
16   public static void openURL(String JavaDoc url) throws Exception JavaDoc {
17      String JavaDoc osName = System.getProperty("os.name");
18      if (osName.startsWith("Mac OS")){
19        Class JavaDoc macUtils = Class.forName("com.apple.mrj.MRJFileUtils");
20        Method JavaDoc openURL = macUtils.getDeclaredMethod("openURL", new Class JavaDoc[] {String JavaDoc.class});
21        openURL.invoke(null, new Object JavaDoc[] {url});
22      } else if (osName.startsWith("Windows"))
23        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
24      else { //assume Unix or Linux
25
String JavaDoc[] browsers = { "firefox", "opera", "konqueror", "mozilla", "netscape" };
26        String JavaDoc browser = null;
27        for (int count = 0; count < browsers.length && browser == null; count++)
28          if (Runtime.getRuntime().exec( new String JavaDoc[] {"which", browsers[count]}).waitFor() == 0)
29            browser = browsers[count];
30          if (browser == null)
31            throw new Exception JavaDoc("Could not find web browser.");
32          else Runtime.getRuntime().exec(new String JavaDoc[] {browser, url});
33      }
34    }
35 }
36
Popular Tags