Java Invoke Browser open page

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.