01: package net.sourceforge.squirrel_sql.fw.util;
02:
03: /////////////////////////////////////////////////////////
04: //Bare Bones Browser Launch //
05: //Version 1.5 //
06: //December 10, 2005 //
07: //Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
08: //Example Usage: //
09: // String url = "http://www.centerkey.com/"; //
10: // BareBonesBrowserLaunch.openURL(url); //
11: //Public Domain Software -- Free to Use as You Like //
12: /////////////////////////////////////////////////////////
13:
14: import java.lang.reflect.Method;
15: import javax.swing.JOptionPane;
16:
17: public class BareBonesBrowserLaunch {
18:
19: private static final String errMsg = "Error attempting to launch web browser";
20:
21: public static void openURL(String url) {
22: String osName = System.getProperty("os.name");
23: try {
24: if (osName.startsWith("Mac OS")) {
25: Class<?> fileMgr = Class
26: .forName("com.apple.eio.FileManager");
27: Method openURL = fileMgr.getDeclaredMethod("openURL",
28: new Class[] { String.class });
29: openURL.invoke(null, new Object[] { url });
30: } else if (osName.startsWith("Windows"))
31: Runtime.getRuntime().exec(
32: "rundll32 url.dll,FileProtocolHandler " + url);
33: else { //assume Unix or Linux
34: String[] browsers = { "firefox", "opera", "konqueror",
35: "epiphany", "mozilla", "netscape" };
36: String browser = null;
37: for (int count = 0; count < browsers.length
38: && browser == null; count++)
39: if (Runtime.getRuntime().exec(
40: new String[] { "which", browsers[count] })
41: .waitFor() == 0)
42: browser = browsers[count];
43: if (browser == null)
44: throw new Exception("Could not find web browser");
45: else
46: Runtime.getRuntime().exec(
47: new String[] { browser, url });
48: }
49: } catch (Exception e) {
50: JOptionPane.showMessageDialog(null, errMsg + ":\n"
51: + e.getLocalizedMessage());
52: }
53: }
54:
55: /**
56: * Test driver
57: *
58: * @param args the first string in this array is the website url to launch.
59: */
60: public static void main(String[] args) {
61: BareBonesBrowserLaunch.openURL(args[0]);
62: }
63:
64: }
|