Open URLs in Java swing using a browser

Source: Internet
Author: User

Open URLs using your browser

    //  bare bones  Browser Launch                           //    //   Version 3.1  (june 6, 2010)                           //     //  By Dem Pilafian                                      //    //  Supports:                                             / /    //     mac os x, gnu/linux, unix,  windows xp/vista/7   //    //  example usage:                                       //     //     String url =  "http://www.centerkey.com/ ";       //    //      Barebonesbrowserlaunch.openurl (URL);             //     //  public domain software -- free to use  as you like  //    /////////////////////////////////////////////////////////     package com.centerkey.bareBonesBrowserLaunch;    import java.util.Arrays;     import javax.swing.JOptionPane;    public class  barebonesbrowserlaunch {    static final string[] browsers =  {  "Google-chrome",  "Firefox",  "opera",     "Epiphany",  "Konqueror" ", " Conkeror ", " Midori ", " Kazehakase ",    " Mozilla " };     static final String errMsg =  "Error attempting to launch  web browser ";     public static void openurl (String url)  {    try { // attempt to use desktop library  from jdk 1.6+     class<?> d = class.forname ("Java.awt.Desktop");     d.getdeclaredmethod ("Browse",  new class[] { java.net.uri.class })      .invoke (D.getdeclaredmethod ("Getdesktop"). Invoke (null),     new object[]  { java.net.uri.create (URL)  });     // above code mimicks:  java.awt.desktop.getdesktop (). browse ()     } catch  (Exception ignore)  { // library not available or failed    String  Osname = system.getproperty ("Os.name");     try {    if   (Osname.startswith ("Mac os"))  {    class.forname (" Com.apple.eio.FileManager ")     .getdeclaredmethod (" OpenURL ",     new  class[] { string.class })  &Nbsp;  .invoke (NULL,&NBSP;NEW&NBSP;OBJECT[]&NBSP;{&NBSP;URL&NBSP;});    }  else if  (Osname.startswith ("Windows"))     runtime.getruntime (). EXEC (      "rundll32 url.dll,fileprotocolhandler "  + url);     else  { // assume unix or linux    string browser =  null;    for  (string b : browsers)     if   (Browser == null    && runtime.getruntime ()      .exec (new string[] {  "which",  b })     .getinputstream (). Read ()  != -1)     runtime.getruntime (). EXEC (    new  string[] { browser = b, url });    if  (browser ==  null)    &Nbsp;throw new exception (arrays.tostring (browsers));    }     } catch  (exception e)  {    joptionpane.showmessagedialog (NULL,     errMsg +  "\ n"  + e.tostring ());    }     }    }    }


Open URLs in Java swing using a browser

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.