001package runwar; 002 003import java.lang.reflect.Method; 004import javax.swing.JOptionPane; 005 006public class BrowserOpener { 007 008 private static final String errMsg = "Error attempting to launch web browser"; 009 010 public static void main(String[] args) throws Exception { 011 openURL(args[0]); 012 } 013 014 public static void openURL(String url) { 015 String osName = System.getProperty("os.name"); 016 if(url == null) { 017 System.out.println("ERROR: No URL specified to open the browser to!"); 018 return; 019 } 020 try { 021 System.out.println(url); 022 if (osName.startsWith("Mac OS")) { 023 Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); 024 Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); 025 openURL.invoke(null, new Object[] { url }); 026 } else if (osName.startsWith("Windows")) 027 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); 028 else { // assume Unix or Linux 029 String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; 030 String browser = null; 031 for (int count = 0; count < browsers.length && browser == null; count++) 032 if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) 033 browser = browsers[count]; 034 if (browser == null) 035 throw new Exception("Could not find web browser"); 036 else 037 Runtime.getRuntime().exec(new String[] { browser, url }); 038 } 039 } catch (Exception e) { 040 e.printStackTrace(); 041 JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage()); 042 } 043 } 044 045}