| 1 | // Written by Dem Pilafian. |
| 2 | // Public domain. |
| 3 | package com.centerkey.utils; |
| 4 | |
| 5 | import java.io.IOException; |
| 6 | import java.lang.reflect.Method; |
| 7 | import java.util.Arrays; |
| 8 | |
| 9 | /** |
| 10 | * <p> |
| 11 | * |
| 12 | * Utility class to open a web page from a Swing application in the user's default browser. On JDK |
| 13 | * 1.6+ it uses <code>java.awt.Desktop.getDesktop().browse()</code>, on earlier versions it |
| 14 | * provides fall backs for Mac OS X, GNU/Linux, Unix, Windows XP/Vista/7.</p> <p> |
| 15 | * |
| 16 | * Example usage:<code>BareBonesBrowserLaunch.openURL("http://example.com/");</code></p> <p> |
| 17 | * |
| 18 | * Latest version: <a href="http://www.centerkey.com/java/browser/"> |
| 19 | * http://www.centerkey.com/java/browser</a> .</p> <p> |
| 20 | * |
| 21 | * Public Domain Software -- Free to Use as You Like.</p> |
| 22 | * |
| 23 | * @author Dem Pilafian (original version) |
| 24 | * @author Thomas Aglassinger (minor fixes, IOException instead of dialog, support for JDK 1.4) |
| 25 | * @version 3.2, April 23 2011 |
| 26 | */ |
| 27 | public final class BareBonesBrowserLaunch |
| 28 | { |
| 29 | private static final String[] BROWSERS = { |
| 30 | "google-chrome", "firefox", "opera", "epiphany", "konqueror", "conkeror", "midori", "kazehakase", "mozilla" |
| 31 | }; |
| 32 | |
| 33 | private BareBonesBrowserLaunch() { } |
| 34 | |
| 35 | /** |
| 36 | * Open <code>url</code> in the user's default browser. |
| 37 | * |
| 38 | * @throws IOException in case the browser cannot be opened |
| 39 | * @param url URL to open, for instance "http://example.com/". |
| 40 | */ |
| 41 | public static void openURL(String url) |
| 42 | throws IOException { |
| 43 | assert url != null; |
| 44 | try { |
| 45 | // Attempt to use Desktop library from JDK 1.6+ by mimicking |
| 46 | // Desktop.getDesktop().browse(java.net.URI.create(url)). |
| 47 | Class desktopClass = Class.forName("java.awt.Desktop"); |
| 48 | Method getDesktopMethod = desktopClass.getDeclaredMethod("getDesktop", new Class[]{}); |
| 49 | Object desktop = getDesktopMethod.invoke(null, new Object[]{}); |
| 50 | Method browseMethod = desktop.getClass().getDeclaredMethod("browse", new Class[]{java.net.URI.class}); |
| 51 | |
| 52 | browseMethod.invoke(null, new Object[]{java.net.URI.create(url)}); |
| 53 | } catch (Exception errorToIgnore) { |
| 54 | // Attempt to use Desktop in JDK 1.6+ failed, try another approach. |
| 55 | String osName = System.getProperty("os.name"); |
| 56 | |
| 57 | try { |
| 58 | if (osName.startsWith("Mac OS")) { |
| 59 | Class fileManagerClass = Class.forName("com.apple.eio.FileManager"); |
| 60 | Method openUrlMethod = fileManagerClass.getDeclaredMethod("openURL", new Class[]{String.class}); |
| 61 | |
| 62 | openUrlMethod.invoke(null, new Object[]{url}); |
| 63 | } else { |
| 64 | Runtime runtime = Runtime.getRuntime(); |
| 65 | |
| 66 | if (osName.startsWith("Windows")) { |
| 67 | runtime.exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", url}); |
| 68 | } else { |
| 69 | // Assume Unix or Linux and try to find an installed browser. |
| 70 | String browserToUse = null; |
| 71 | int browserToTryIndex = 0; |
| 72 | |
| 73 | while ((browserToUse == null) && (browserToTryIndex < BROWSERS.length)) { |
| 74 | String browserToTry = BROWSERS[browserToTryIndex]; |
| 75 | |
| 76 | Process whichProcess = runtime.exec(new String[]{"which", browserToTry}); |
| 77 | boolean browserToTryIsInstalled = (whichProcess.exitValue() == 0); |
| 78 | |
| 79 | if (browserToTryIsInstalled) { |
| 80 | browserToUse = browserToTry; |
| 81 | } else { |
| 82 | browserToTryIndex += 1; |
| 83 | } |
| 84 | } |
| 85 | if (browserToUse == null) { |
| 86 | throw new IOException("one of the following web browsers must be installed: " |
| 87 | + Arrays.toString(BROWSERS)); |
| 88 | } |
| 89 | runtime.exec(new String[]{browserToUse, url}); |
| 90 | } |
| 91 | } |
| 92 | } catch (Exception error) { |
| 93 | throw new IOException("cannot open URL: " + url, error); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |