KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > manager > BrowserControl


1 package hero.client.manager;
2
3 import java.io.IOException JavaDoc;
4
5 /**
6 * A simple, static class to display a URL in the system browser.
7
8
9 *
10 * Under Unix, the system browser is hard-coded to be 'netscape'.
11 * Netscape must be in your PATH for this to work. This has been
12 * tested with the following platforms: AIX, HP-UX and Solaris.
13
14
15 *
16 * Under Windows, this will bring up the default browser under windows,
17 * usually either Netscape or Microsoft IE. The default browser is
18 * determined by the OS. This has been tested under Windows 95/98/NT.
19
20
21 *
22 * Examples:
23
24
25 *
26 BrowserControl.displayURL("http://www.javaworld.com")
27 *
28 BrowserControl.displayURL("file://c:\\docs\\index.html")
29 *
30 BrowserContorl.displayURL("file:///user/joe/index.html");
31 *
32
33 * Note - you must include the url type -- either "http://" or
34 * "file://".
35 */

36 public class BrowserControl
37 {
38
39 static java.util.ResourceBundle JavaDoc resource = java.util.ResourceBundle.getBundle("resources.Traduction")/*#BundleType=List*/;
40
41     /**
42      * Display a file in the system browser. If you want to display a
43      * file, you must include the absolute path name.
44      *
45      * @param url the file's url (the url must start with either "http://"
46 or
47      * "file://").
48      */

49     public static void displayURL(String JavaDoc url)
50     {
51         boolean windows = isWindowsPlatform();
52         String JavaDoc cmd = null;
53         try
54         {
55             if (windows)
56             {
57                 // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
58
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
59                 Process JavaDoc p = Runtime.getRuntime().exec(cmd);
60             }
61             else
62             {
63                 // Under Unix, Netscape has to be running for the "-remote"
64
// command to work. So, we try sending the command and
65
// check for an exit value. If the exit command is 0,
66
// it worked, otherwise we need to start the browser.
67
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
68
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
69                 Process JavaDoc p = Runtime.getRuntime().exec(cmd);
70                 try
71                 {
72                     // wait for exit code -- if it's 0, command worked,
73
// otherwise we need to start the browser up.
74
int exitCode = p.waitFor();
75                     if (exitCode != 0)
76                     {
77                         // Command failed, start up the browser
78
// cmd = 'netscape http://www.javaworld.com'
79
cmd = UNIX_PATH + " " + url;
80                         p = Runtime.getRuntime().exec(cmd);
81                     }
82                 }
83                 catch(InterruptedException JavaDoc x)
84                 {
85                     System.err.println(resource.getString("browsercontrol.error") +
86                                        cmd + "'");
87                     System.err.println(resource.getString("browsercontrol.caught") + x);
88                 }
89             }
90         }
91         catch(IOException JavaDoc x)
92         {
93             // couldn't exec browser
94
System.err.println(resource.getString("browsercontrol.notinvoke") + cmd);
95             System.err.println(resource.getString("browsercontrol.caught") + x);
96         }
97     }
98     /**
99      * Try to determine whether this application is running under Windows
100      * or some other platform by examing the "os.name" property.
101      *
102      * @return true if this application is running under a Windows OS
103      */

104     public static boolean isWindowsPlatform()
105     {
106         String JavaDoc os = System.getProperty("os.name");
107         if ( os != null && os.startsWith(WIN_ID))
108             return true;
109         else
110             return false;
111
112     }
113     /**
114      * Simple example.
115      */

116     public static void main(String JavaDoc[] args)
117     {
118         displayURL("http://www.javaworld.com");
119     }
120     // Used to identify the windows platform.
121
private static final String JavaDoc WIN_ID = "Windows";
122     // The default system browser under windows.
123
private static final String JavaDoc WIN_PATH = "rundll32";
124     // The flag to display a url.
125
private static final String JavaDoc WIN_FLAG = "url.dll,FileProtocolHandler";
126     // The default browser under unix.
127
private static final String JavaDoc UNIX_PATH = "netscape";
128     // The flag to display a url.
129
private static final String JavaDoc UNIX_FLAG = "-remote openURL";
130 }
131
Popular Tags