Skip to content

Instantly share code, notes, and snippets.

@mrWinston
Last active May 6, 2016 09:21
Show Gist options
  • Save mrWinston/38432f3ef5c8a2279f81 to your computer and use it in GitHub Desktop.
Save mrWinston/38432f3ef5c8a2279f81 to your computer and use it in GitHub Desktop.
Open Browser window in Java #Java #browser #Runtime #webpage
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/
public static void openWebpage(String url, String... browsers) throws IOException {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf( "win" ) >= 0 ){
Runtime rt = Runtime.getRuntime();
rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
}else if(os.indexOf( "mac" ) >= 0){
Runtime rt = Runtime.getRuntime();
rt.exec( "open" + url);
}else if(os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0){
Runtime rt = Runtime.getRuntime();
// String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
// "netscape","opera","links","lynx"};
StringBuffer cmd = new StringBuffer();
for (int i=0; i<browsers.length; i++)
cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");
rt.exec(new String[] { "sh", "-c", cmd.toString() });
}else {
throw new UnsupportedOperationException("Can Not Determine OS!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment