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