Open a webpage in Java

Source: Internet
Author: User
Document directory
  • 1. Use
Barebonesbrowserlaunch. Java is a tool class inadvertently seen on the Internet. It means opening a URL, which is common in different operating systems. 1. Use

Simple:

Java code
1   String url = "http://www.google.com/";         
2 BareBonesBrowserLaunch.openURL(url);
2. The following is the source code of barebonesbrowserlaunch. java. Although it was written by someone else, it became your own. Java code
1 /////////////////////////////////////// //////////////////
2 // bare bones browser launch //
3 // version 1.5 (December 10,200 5 )//
4 // by DEM pilafian //
5 // supported: Mac OS X, GNU/Linux, UNIX, Windows XP //
6 // available for free //
7 /////////////////////////////////////// //////////////////
8
9 /**
10 * @ author DEM pilafian
11 * @ author john Kristian
12 */
13 Import java. Io. ioexception;
14 Import java. Lang. Reflect. invocationtargetexception;
15 Import java. Lang. Reflect. method;
16 Import javax. Swing. joptionpane;
17
18 public class barebonesbrowserlaunch {
19
20 public static void Openurl (string URL ){
21 try {
22 browse (URL );
23} catch (exception e ){
24}
25}
26
27 Private Static void browse (string URL) throws exception {
28 // obtain the operating system name
29 string osname = system. getproperty ("OS. Name ","");
30 if (osname. startswith ("Mac OS ")){
31 // how Apple opens
32 class filemgr = Class. forname ("com. Apple. EIO. filemanager ");
33 method Openurl = filemgr. getdeclaredmethod ("Openurl", new class [] {string. Class });
34 Openurl. Invoke (null, new object [] {URL });
35} else if (osname. startswith ("Windows ")){
36 // how to enable Windows.
37 runtime.getruntime(cmd.exe C ("rundll32 URL. dll, fileprotocolhandler" + URL );
38} else {
39 // how to enable Unix or Linux
40 string [] browsers = {"Firefox", "Opera", "Konqueror", "Epiphany", "Mozilla", "Netscape "};
41 string browser = NULL;
42 for (int count = 0; count <browsers. Length & browser = NULL; count ++)
43 // execute the code and jump out after Brower has a value,
44 // if the process is successfully created, = 0 indicates that the process ends normally.
45 if (runtime.getruntime(cmd.exe C (New String [] {"which", browsers [count]}). waitfor () = 0)
46 browser = browsers [count];
47 If (Browser = NULL)
48 throw new exception ("cocould not find Web Browser ");
49 else
50 // This value has successfully obtained a process.
51 runtime.getruntime(cmd.exe C (New String [] {browser, URL });
52}
53}
54}

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.