PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/org/virion/jam/util/BrowserLauncher.java

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