Open browser in older version of Java -
i trying open uri in browser java application. know in current version of java can done using java.awt.desktop, class not available in earlier versions of java, such 1.5, using. there way open default browser given uri in earlier versions of java?
i think should trick
import java.lang.reflect.method; import javax.swing.joptionpane; public class barebonesbrowserlaunch { private static final string errmsg = "error attempting launch web browser"; public static void openurl(string url) { string osname = system.getproperty("os.name"); try { if (osname.startswith("mac os")) { class filemgr = class.forname("com.apple.eio.filemanager"); method openurl = filemgr.getdeclaredmethod("openurl", new class[] {string.class}); openurl.invoke(null, new object[] {url}); } else if (osname.startswith("windows")) { runtime.getruntime().exec("rundll32 url.dll,fileprotocolhandler " + url); } else { //assume unix or linux string[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; string browser = null; (int count = 0; count < browsers.length && browser == null; count++) { if (runtime.getruntime().exec(new string[] {"which", browsers[count]}).waitfor() == 0) { browser = browsers[count]; } } if (browser == null) { throw new exception("could not find web browser"); } else { runtime.getruntime().exec(new string[] {browser, url}); } } } catch (exception e) { joptionpane.showmessagedialog(null, errmsg + ":\n" + e.getlocalizedmessage()); } } }
Comments
Post a Comment