1 package tim.prune.browser;
3 import javax.swing.JOptionPane;
7 * Class to launch a browser window to show an external map
9 public class BrowserLauncher
11 private String[] _browserCommand = null;
12 private boolean _urlNeedsQuotes = false;
15 * Constructor to set up browser
17 public BrowserLauncher()
19 // First check if "which" command is available
20 if (commandExists("which"))
22 // which exists, so try browsers in turn
23 String[] browsersToTry = {"firefox", "iceweasel", "konqueror", "opera", "epiphany", "mozilla", "safari", "lynx"};
24 String browserFound = null;
25 for (int i=0; i<browsersToTry.length && browserFound == null; i++)
27 if (commandExists(browsersToTry[i]))
28 browserFound = browsersToTry[i];
30 if (browserFound != null) {
31 _browserCommand = new String[] {browserFound, null};
36 // no which command, so check if os name looks like a mac
37 boolean isMacOsx = System.getProperty("os.name").toLowerCase().indexOf("mac os") >= 0;
39 // for Mac Osx just use "open" command
40 _browserCommand = new String[] {"open", null};
43 // assume it's not linux or mac, so try windows method using "start" command
44 _browserCommand = new String[] {"cmd.exe", "/C", "start", "\"\"", null};
45 _urlNeedsQuotes = true;
51 * Check if the specified command exists on the system
52 * @param inCommand command to check
53 * @return true if the command exists
55 private static boolean commandExists(String inCommand)
59 String[] commands = {"which", inCommand};
60 if (Runtime.getRuntime().exec(commands).waitFor() == 0)
65 catch (Exception e) {} // failed
70 * Launch a browser window to show the given url
71 * @param inUrl url to show
73 public void launchBrowser(String inUrl)
75 if (_browserCommand == null) {
76 JOptionPane.showMessageDialog(null, "Cannot show url: " + inUrl);
82 // enclose url in quotes if necessary
84 if (_urlNeedsQuotes) {url = "\"" + url + "\"";}
85 // Fill in url in last element of coommand array
86 _browserCommand[_browserCommand.length - 1] = url;
87 // execute command to launch browser
88 Runtime.getRuntime().exec(_browserCommand);
91 JOptionPane.showMessageDialog(null, "Failed to show url: " + inUrl);