firefox now default for unixen.
[jalview.git] / src / jalview / util / BrowserLauncher.java
1 /********************
2  * 2004 Jalview Reengineered
3  * Barton Group
4  * Dundee University
5  *
6  * AM Waterhouse
7  *******************/
8
9 package jalview.util;
10
11 import java.io.File;
12 import java.io.IOException;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.Field;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17
18 /**
19  * BrowserLauncher is a class that provides one static method, openURL, which opens the default
20  * web browser for the current user of the system to the given URL.  It may support other
21  * protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously
22  * tested and is not guaranteed to work.
23  * <p>
24  * Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms
25  * that are not part of the standard JDK.  What we're trying to do, though, is to take something
26  * that's frequently desirable but inherently platform-specific -- opening a default browser --
27  * and allow programmers (you, for example) to do so without worrying about dropping into native
28  * code or doing anything else similarly evil.
29  * <p>
30  * Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without
31  * modification or a need for additional libraries.  All classes that are required on certain
32  * platforms to allow this to run are dynamically loaded at runtime via reflection and, if not
33  * found, will not cause this to do anything other than returning an error when opening the
34  * browser.
35  * <p>
36  * There are certain system requirements for this class, as it's running through Runtime.exec(),
37  * which is Java's way of making a native system call.  Currently, this requires that a Macintosh
38  * have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that
39  * have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder
40  * in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and
41  * 8.1), and for all Mac OS 8.5 and later systems.  On Windows, it only runs under Win32 systems
42  * (Windows 95, 98, and NT 4.0, as well as later versions of all).  On other systems, this drops
43  * back from the inherently platform-sensitive concept of a default browser and simply attempts
44  * to launch Netscape via a shell command.
45  * <p>
46  * This code is Copyright 1999-2001 by Eric Albert (ejalbert\@cs.stanford.edu) and may be
47  * redistributed or modified in any form without restrictions as long as the portion of this
48  * comment from this paragraph through the end of the comment is not removed.  The author
49  * requests that he be notified of any application, applet, or other binary that makes use of
50  * this code, but that's more out of curiosity than anything and is not required.  This software
51  * includes no warranty.  The author is not repsonsible for any loss of data or functionality
52  * or any adverse or unexpected effects of using this software.
53  * <p>
54  * Credits:
55  * <br>Steven Spencer, JavaWorld magazine (<a href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip 66</a>)
56  * <br>Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore,
57  * Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
58  *
59  * @author Eric Albert (<a href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
60  * @version 1.4b1 (Released June 20, 2001)
61  */
62 public class BrowserLauncher {
63
64         /**
65          * The Java virtual machine that we are running on.  Actually, in most cases we only care
66          * about the operating system, but some operating systems require us to switch on the VM. */
67         private static int jvm;
68
69         /** The browser for the system */
70         private static Object browser;
71
72         /**
73          * Caches whether any classes, methods, and fields that are not part of the JDK and need to
74          * be dynamically loaded at runtime loaded successfully.
75          * <p>
76          * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
77          * IOException.
78          */
79         private static boolean loadedWithoutErrors;
80
81         /** The com.apple.mrj.MRJFileUtils class */
82         private static Class mrjFileUtilsClass;
83
84         /** The com.apple.mrj.MRJOSType class */
85         private static Class mrjOSTypeClass;
86
87         /** The com.apple.MacOS.AEDesc class */
88         private static Class aeDescClass;
89
90         /** The &lt;init&gt;(int) method of com.apple.MacOS.AETarget */
91         private static Constructor aeTargetConstructor;
92
93         /** The &lt;init&gt;(int, int, int) method of com.apple.MacOS.AppleEvent */
94         private static Constructor appleEventConstructor;
95
96         /** The &lt;init&gt;(String) method of com.apple.MacOS.AEDesc */
97         private static Constructor aeDescConstructor;
98
99         /** The findFolder method of com.apple.mrj.MRJFileUtils */
100         private static Method findFolder;
101
102         /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
103         private static Method getFileCreator;
104
105         /** The getFileType method of com.apple.mrj.MRJFileUtils */
106         private static Method getFileType;
107
108         /** The openURL method of com.apple.mrj.MRJFileUtils */
109         private static Method openURL;
110
111         /** The makeOSType method of com.apple.MacOS.OSUtils */
112         private static Method makeOSType;
113
114         /** The putParameter method of com.apple.MacOS.AppleEvent */
115         private static Method putParameter;
116
117         /** The sendNoReply method of com.apple.MacOS.AppleEvent */
118         private static Method sendNoReply;
119
120         /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
121         private static Object kSystemFolderType;
122
123         /** The keyDirectObject AppleEvent parameter type */
124         private static Integer keyDirectObject;
125
126         /** The kAutoGenerateReturnID AppleEvent code */
127         private static Integer kAutoGenerateReturnID;
128
129         /** The kAnyTransactionID AppleEvent code */
130         private static Integer kAnyTransactionID;
131
132         /** The linkage object required for JDirect 3 on Mac OS X. */
133         private static Object linkage;
134
135         /** The framework to reference on Mac OS X */
136         private static final String JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
137
138         /** JVM constant for MRJ 2.0 */
139         private static final int MRJ_2_0 = 0;
140
141         /** JVM constant for MRJ 2.1 or later */
142         private static final int MRJ_2_1 = 1;
143
144         /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
145         private static final int MRJ_3_0 = 3;
146
147         /** JVM constant for MRJ 3.1 */
148         private static final int MRJ_3_1 = 4;
149
150         /** JVM constant for any Windows NT JVM */
151         private static final int WINDOWS_NT = 5;
152
153         /** JVM constant for any Windows 9x JVM */
154         private static final int WINDOWS_9x = 6;
155
156         /** JVM constant for any other platform */
157         private static final int OTHER = -1;
158
159         /**
160          * The file type of the Finder on a Macintosh.  Hardcoding "Finder" would keep non-U.S. English
161          * systems from working properly.
162          */
163         private static final String FINDER_TYPE = "FNDR";
164
165         /**
166          * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
167          * application.
168          */
169         private static final String FINDER_CREATOR = "MACS";
170
171         /** The name for the AppleEvent type corresponding to a GetURL event. */
172         private static final String GURL_EVENT = "GURL";
173
174         /**
175          * The first parameter that needs to be passed into Runtime.exec() to open the default web
176          * browser on Windows.
177          */
178     private static final String FIRST_WINDOWS_PARAMETER = "/c";
179
180     /** The second parameter for Runtime.exec() on Windows. */
181     private static final String SECOND_WINDOWS_PARAMETER = "start";
182
183     /**
184      * The third parameter for Runtime.exec() on Windows.  This is a "title"
185      * parameter that the command line expects.  Setting this parameter allows
186      * URLs containing spaces to work.
187      */
188     private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
189
190         /**
191          * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
192          * on many command-line systems.
193          */
194         private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
195         private static final String NETSCAPE_OPEN_PARAMETER_START = "openURL(";
196         private static final String NETSCAPE_OPEN_NEW_WINDOW = ", new-window";
197         private static final String NETSCAPE_OPEN_PARAMETER_END = ")";
198
199         /**
200          * The message from any exception thrown throughout the initialization process.
201          */
202         private static String errorMessage;
203
204         /**
205          * An initialization block that determines the operating system and loads the necessary
206          * runtime data.
207          */
208         static {
209                 loadedWithoutErrors = true;
210                 String osName = System.getProperty("os.name");
211                 if (osName.startsWith("Mac OS")) {
212                         String mrjVersion = System.getProperty("mrj.version");
213                         String majorMRJVersion = mrjVersion.substring(0, 3);
214                         try {
215                                 double version = Double.valueOf(majorMRJVersion).doubleValue();
216                                 if (version == 2) {
217                                         jvm = MRJ_2_0;
218                                 } else if (version >= 2.1 && version < 3) {
219                                         // Assume that all 2.x versions of MRJ work the same.  MRJ 2.1 actually
220                                         // works via Runtime.exec() and 2.2 supports that but has an openURL() method
221                                         // as well that we currently ignore.
222                                         jvm = MRJ_2_1;
223                                 } else if (version == 3.0) {
224                                         jvm = MRJ_3_0;
225                                 } else if (version >= 3.1) {
226                                         // Assume that all 3.1 and later versions of MRJ work the same.
227                                         jvm = MRJ_3_1;
228                                 } else {
229                                         loadedWithoutErrors = false;
230                                         errorMessage = "Unsupported MRJ version: " + version;
231                                 }
232                         } catch (NumberFormatException nfe) {
233                                 loadedWithoutErrors = false;
234                                 errorMessage = "Invalid MRJ version: " + mrjVersion;
235                         }
236                 } else if (osName.startsWith("Windows")) {
237                         if (osName.indexOf("9") != -1) {
238                                 jvm = WINDOWS_9x;
239                         } else {
240                                 jvm = WINDOWS_NT;
241                         }
242                 } else {
243                         jvm = OTHER;
244                 }
245
246                 if (loadedWithoutErrors) {      // if we haven't hit any errors yet
247                         loadedWithoutErrors = loadClasses();
248                 }
249         }
250
251         /**
252          * This class should be never be instantiated; this just ensures so.
253          */
254         private BrowserLauncher() { }
255
256         /**
257          * Called by a static initializer to load any classes, fields, and methods required at runtime
258          * to locate the user's web browser.
259          * @return <code>true</code> if all intialization succeeded
260          *                      <code>false</code> if any portion of the initialization failed
261          */
262         private static boolean loadClasses() {
263                 switch (jvm) {
264                         case MRJ_2_0:
265                                 try {
266                                         Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
267                                         Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
268                                         Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
269                                         Class aeClass = Class.forName("com.apple.MacOS.ae");
270                                         aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
271
272                                         aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class [] { int.class });
273                                         appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
274                                         aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });
275
276                                         makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class [] { String.class });
277                                         putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass });
278                                         sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] { });
279
280                                         Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
281                                         keyDirectObject = (Integer) keyDirectObjectField.get(null);
282                                         Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
283                                         kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
284                                         Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
285                                         kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
286                                 } catch (ClassNotFoundException cnfe) {
287                                         errorMessage = cnfe.getMessage();
288                                         return false;
289                                 } catch (NoSuchMethodException nsme) {
290                                         errorMessage = nsme.getMessage();
291                                         return false;
292                                 } catch (NoSuchFieldException nsfe) {
293                                         errorMessage = nsfe.getMessage();
294                                         return false;
295                                 } catch (IllegalAccessException iae) {
296                                         errorMessage = iae.getMessage();
297                                         return false;
298                                 }
299                                 break;
300                         case MRJ_2_1:
301                                 try {
302                                         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
303                                         mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
304                                         Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
305                                         kSystemFolderType = systemFolderField.get(null);
306                                         findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
307                                         getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
308                                         getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
309                                 } catch (ClassNotFoundException cnfe) {
310                                         errorMessage = cnfe.getMessage();
311                                         return false;
312                                 } catch (NoSuchFieldException nsfe) {
313                                         errorMessage = nsfe.getMessage();
314                                         return false;
315                                 } catch (NoSuchMethodException nsme) {
316                                         errorMessage = nsme.getMessage();
317                                         return false;
318                                 } catch (SecurityException se) {
319                                         errorMessage = se.getMessage();
320                                         return false;
321                                 } catch (IllegalAccessException iae) {
322                                         errorMessage = iae.getMessage();
323                                         return false;
324                                 }
325                                 break;
326                         case MRJ_3_0:
327                             try {
328                                         Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
329                                         Constructor constructor = linker.getConstructor(new Class[]{ Class.class });
330                                         linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
331                                 } catch (ClassNotFoundException cnfe) {
332                                         errorMessage = cnfe.getMessage();
333                                         return false;
334                                 } catch (NoSuchMethodException nsme) {
335                                         errorMessage = nsme.getMessage();
336                                         return false;
337                                 } catch (InvocationTargetException ite) {
338                                         errorMessage = ite.getMessage();
339                                         return false;
340                                 } catch (InstantiationException ie) {
341                                         errorMessage = ie.getMessage();
342                                         return false;
343                                 } catch (IllegalAccessException iae) {
344                                         errorMessage = iae.getMessage();
345                                         return false;
346                                 }
347                                 break;
348                         case MRJ_3_1:
349                                 try {
350                                         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
351                                         openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
352                                 } catch (ClassNotFoundException cnfe) {
353                                         errorMessage = cnfe.getMessage();
354                                         return false;
355                                 } catch (NoSuchMethodException nsme) {
356                                         errorMessage = nsme.getMessage();
357                                         return false;
358                                 }
359                                 break;
360                         default:
361                             break;
362                 }
363                 return true;
364         }
365
366         /**
367          * Attempts to locate the default web browser on the local system.  Caches results so it
368          * only locates the browser once for each use of this class per JVM instance.
369          * @return The browser for the system.  Note that this may not be what you would consider
370          *                      to be a standard web browser; instead, it's the application that gets called to
371          *                      open the default web browser.  In some cases, this will be a non-String object
372          *                      that provides the means of calling the default browser.
373          */
374         private static Object locateBrowser() {
375                 if (browser != null) {
376                         return browser;
377                 }
378                 switch (jvm) {
379                         case MRJ_2_0:
380                                 try {
381                                         Integer finderCreatorCode = (Integer) makeOSType.invoke(null, new Object[] { FINDER_CREATOR });
382                                         Object aeTarget = aeTargetConstructor.newInstance(new Object[] { finderCreatorCode });
383                                         Integer gurlType = (Integer) makeOSType.invoke(null, new Object[] { GURL_EVENT });
384                                         Object appleEvent = appleEventConstructor.newInstance(new Object[] { gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID });
385                                         // Don't set browser = appleEvent because then the next time we call
386                                         // locateBrowser(), we'll get the same AppleEvent, to which we'll already have
387                                         // added the relevant parameter. Instead, regenerate the AppleEvent every time.
388                                         // There's probably a way to do this better; if any has any ideas, please let
389                                         // me know.
390                                         return appleEvent;
391                                 } catch (IllegalAccessException iae) {
392                                         browser = null;
393                                         errorMessage = iae.getMessage();
394                                         return browser;
395                                 } catch (InstantiationException ie) {
396                                         browser = null;
397                                         errorMessage = ie.getMessage();
398                                         return browser;
399                                 } catch (InvocationTargetException ite) {
400                                         browser = null;
401                                         errorMessage = ite.getMessage();
402                                         return browser;
403                                 }
404                         case MRJ_2_1:
405                                 File systemFolder;
406                                 try {
407                                         systemFolder = (File) findFolder.invoke(null, new Object[] { kSystemFolderType });
408                                 } catch (IllegalArgumentException iare) {
409                                         browser = null;
410                                         errorMessage = iare.getMessage();
411                                         return browser;
412                                 } catch (IllegalAccessException iae) {
413                                         browser = null;
414                                         errorMessage = iae.getMessage();
415                                         return browser;
416                                 } catch (InvocationTargetException ite) {
417                                         browser = null;
418                                         errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
419                                         return browser;
420                                 }
421                                 String[] systemFolderFiles = systemFolder.list();
422                                 // Avoid a FilenameFilter because that can't be stopped mid-list
423                                 for(int i = 0; i < systemFolderFiles.length; i++) {
424                                         try {
425                                                 File file = new File(systemFolder, systemFolderFiles[i]);
426                                                 if (!file.isFile()) {
427                                                         continue;
428                                                 }
429                                                 // We're looking for a file with a creator code of 'MACS' and
430                                                 // a type of 'FNDR'.  Only requiring the type results in non-Finder
431                                                 // applications being picked up on certain Mac OS 9 systems,
432                                                 // especially German ones, and sending a GURL event to those
433                                                 // applications results in a logout under Multiple Users.
434                                                 Object fileType = getFileType.invoke(null, new Object[] { file });
435                                                 if (FINDER_TYPE.equals(fileType.toString())) {
436                                                         Object fileCreator = getFileCreator.invoke(null, new Object[] { file });
437                                                         if (FINDER_CREATOR.equals(fileCreator.toString())) {
438                                                                 browser = file.toString();      // Actually the Finder, but that's OK
439                                                                 return browser;
440                                                         }
441                                                 }
442                                         } catch (IllegalArgumentException iare) {
443                                                 browser = browser;
444                                                 errorMessage = iare.getMessage();
445                                                 return null;
446                                         } catch (IllegalAccessException iae) {
447                                                 browser = null;
448                                                 errorMessage = iae.getMessage();
449                                                 return browser;
450                                         } catch (InvocationTargetException ite) {
451                                                 browser = null;
452                                                 errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
453                                                 return browser;
454                                         }
455                                 }
456                                 browser = null;
457                                 break;
458                         case MRJ_3_0:
459                         case MRJ_3_1:
460                                 browser = "";   // Return something non-null
461                                 break;
462                         case WINDOWS_NT:
463                                 browser = "cmd.exe";
464                                 break;
465                         case WINDOWS_9x:
466                                 browser = "command.com";
467                                 break;
468                         case OTHER:
469                         default:
470                                 browser = jalview.bin.Cache.applicationProperties.getProperty("jalview.browser");
471                                 if (browser==null) {
472                                   // hope firefox exists :-/
473                                   browser = "firefox";
474                                 }
475                                 break;
476                 }
477                 return browser;
478         }
479
480         /**
481          * Attempts to open the default web browser to the given URL.
482          * @param url The URL to open
483          * @throws IOException If the web browser could not be located or does not run
484          */
485         public static void openURL(String url) throws IOException {
486                 if (!loadedWithoutErrors) {
487                         throw new IOException("Exception in finding browser: " + errorMessage);
488                 }
489                 Object browser = locateBrowser();
490                 if (browser == null) {
491                         throw new IOException("Unable to locate browser: " + errorMessage);
492                 }
493
494                 switch (jvm) {
495                         case MRJ_2_0:
496                                 Object aeDesc = null;
497                                 try {
498                                         aeDesc = aeDescConstructor.newInstance(new Object[] { url });
499                                         putParameter.invoke(browser, new Object[] { keyDirectObject, aeDesc });
500                                         sendNoReply.invoke(browser, new Object[] { });
501                                 } catch (InvocationTargetException ite) {
502                                         throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
503                                 } catch (IllegalAccessException iae) {
504                                         throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
505                                 } catch (InstantiationException ie) {
506                                         throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
507                                 } finally {
508                                         aeDesc = null;  // Encourage it to get disposed if it was created
509                                         browser = null; // Ditto
510                                 }
511                                 break;
512                         case MRJ_2_1:
513                                 Runtime.getRuntime().exec(new String[] { (String) browser, url } );
514                                 break;
515                         case MRJ_3_0:
516                                 int[] instance = new int[1];
517                                 int result = ICStart(instance, 0);
518                                 if (result == 0) {
519                                         int[] selectionStart = new int[] { 0 };
520                                         byte[] urlBytes = url.getBytes();
521                                         int[] selectionEnd = new int[] { urlBytes.length };
522                                         result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
523                                                                                         urlBytes.length, selectionStart,
524                                                                                         selectionEnd);
525                                         if (result == 0) {
526                                                 // Ignore the return value; the URL was launched successfully
527                                                 // regardless of what happens here.
528                                                 ICStop(instance);
529                                         } else {
530                                                 throw new IOException("Unable to launch URL: " + result);
531                                         }
532                                 } else {
533                                         throw new IOException("Unable to create an Internet Config instance: " + result);
534                                 }
535                                 break;
536                         case MRJ_3_1:
537                                 try {
538                                         openURL.invoke(null, new Object[] { url });
539                                 } catch (InvocationTargetException ite) {
540                                         throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
541                                 } catch (IllegalAccessException iae) {
542                                         throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
543                                 }
544                                 break;
545                     case WINDOWS_NT:
546                     case WINDOWS_9x:
547                             // Add quotes around the URL to allow ampersands and other special
548                             // characters to work.
549                                 Process process = Runtime.getRuntime().exec(new String[] { (String) browser,
550                                                                                                                                 FIRST_WINDOWS_PARAMETER,
551                                                                                                                                 SECOND_WINDOWS_PARAMETER,
552                                                                                                                                 THIRD_WINDOWS_PARAMETER,
553                                                                                                                                 '"' + url + '"' });
554                                 // This avoids a memory leak on some versions of Java on Windows.
555                                 // That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
556                                 try {
557                                         process.waitFor();
558                                         process.exitValue();
559                                 } catch (InterruptedException ie) {
560                                         throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
561                                 }
562                                 break;
563                         case OTHER:
564                                 // Assume that we're on Unix and that Netscape (actually Firefox) is installed
565
566                                 // First, attempt to open the URL in a currently running session of Netscape
567                                 process = Runtime.getRuntime().exec(new String[] { (String) browser,
568                                                                                                         NETSCAPE_REMOTE_PARAMETER,
569                                                                                                         NETSCAPE_OPEN_PARAMETER_START +
570                                                                                                         url +
571                                                                                                         NETSCAPE_OPEN_NEW_WINDOW +
572                                                                                                         NETSCAPE_OPEN_PARAMETER_END });
573                                 try {
574                                         int exitCode = process.waitFor();
575                                         if (exitCode != 0) {    // if Netscape was not open
576                                                 Runtime.getRuntime().exec(new String[] { (String) browser, url });
577                                         }
578                                 } catch (InterruptedException ie) {
579                                         throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
580                                 }
581                                 break;
582                         default:
583                                 // This should never occur, but if it does, we'll try the simplest thing possible
584                                 Runtime.getRuntime().exec(new String[] { (String) browser, url });
585                                 break;
586                 }
587         }
588
589         /**
590          * Methods required for Mac OS X.  The presence of native methods does not cause
591          * any problems on other platforms.
592          */
593         private native static int ICStart(int[] instance, int signature);
594         private native static int ICStop(int[] instance);
595         private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
596                                                                                         int[] selectionStart, int[] selectionEnd);
597 }