Launch browser in MAC, Linux, Unix

Source: Internet
Author: User

 

//////////////////////////////////////// /////////////////
// Bare bones browser launch //
// Version 1.5 //
// December 10,200 5 //
// Supports: Mac OS X, GNU/Linux, UNIX, Windows XP //
// Example usage ://
// String url = "http://www.centerkey.com /";//
// Barebonesbrowserlaunch. Openurl (URL );//
// Public domain software -- free to use as you like //
//////////////////////////////////////// /////////////////

Import java. Lang. Reflect. method;
Import javax. Swing. joptionpane;

Public class barebonesbrowserlaunch {

Private Static final string errmsg = "error attempting to 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(cmd.exe C ("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(cmd.exe C (
New String [] {"which", browsers [count]}). waitfor () = 0)
Browser = browsers [count];
If (Browser = NULL)
Throw new exception ("cocould not find Web Browser ");
Else
Runtime.getruntime(cmd.exe C (New String [] {browser, URL });
}
}
Catch (exception e ){
Joptionpane. showmessagedialog (null, errmsg + ":" + E. getlocalizedmessage ());
}
}

}

From: http://www.java2s.com/Code/Java/Development-Class/LaunchBrowserinMacLinuxUnix.htm

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.