public static void OpenURL (String URL) {
try {
browse (URL);
} catch (Exception e) {
Joptionpane.showmessagedialog (NULL, "Error attempting to launch Web browser:\n" + e.getlocalizedmessage ());
}
}
private static void browse (String url) throws ClassNotFoundException, Illegalaccessexception,
IllegalArgumentException, Interruptedexception, InvocationTargetException, IOException,
nosuchmethodexception {
String osname = System.getproperty ("Os.name", "" ");
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;
for (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 Nosuchmethodexception ("Could not find web browser");
Else
Runtime.getruntime (). EXEC (new string[] {browser, url});
}
}
Java Invoke Browser open page