//http://stackoverflow.com/questions/5226212/how-to-open-the-default-webbrowser-using-java //CROSS-PLATFORM SOLUTION import java.awt.Desktop; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class Browser { public static void main(String[] args) { String url = "http://www.google.com"; if(Desktop.isDesktopSupported()){ Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(url)); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Runtime runtime = Runtime.getRuntime(); try { runtime.exec("xdg-open " + url); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //ALTERNATIVE CROSS-PLATFORM SOLUTION import java.awt.Desktop; import java.net.URI; public class App { public static void main(String[] args) throws Exception { String url = "http://stackoverflow.com"; if(Desktop.isDesktopSupported()) { // Windows Desktop.getDesktop().browse(new URI(url)); } else { // Ubuntu Runtime runtime = Runtime.getRuntime(); runtime.exec("/usr/bin/firefox -new-window " + url); } } } // BARE BONES SOLUTION http://centerkey.com/java/browser/ // 1) THE CALL String url = "http://www.google.com"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); // 2) THE CODE import javax.swing.JOptionPane; import java.util.Arrays; public class BareBonesBrowserLaunch { static final String[] browsers = { "x-www-browser", "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"); /* The following statement mimicks: java.awt.Desktop.getDesktop().browse() */ d.getDeclaredMethod("browse",new Class[]{java.net.URI.class}).invoke( d.getDeclaredMethod("getDesktop") .invoke(null),new Object[]{java.net.URI.create(url)}); } 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}) .invoke(null,new Object[]{url}); } else if(osName.startsWith("Windows")) Runtime.getRuntime() .exec("rundll32 url.dll,FileProtocolHandler " + url); /* Assume Linux/Unix */ else { 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) throw new Exception(Arrays.toString(browsers)); } } catch(Exception e){ JOptionPane.showMessageDialog(null,errMsg + "\n" + e.toString()); } } } } // 3) TEST PROGRAM import java.awt.event.*; import javax.swing.*; public class MyApp { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); final JTextField urlField = new JTextField("http://centerkey.com "); JButton webButton = new JButton("Web Trip"); webButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { BareBonesBrowserLaunch.openURL(urlField.getText().trim()); } } ); frame.setTitle("Bare Bones Browser Launch"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.add(new JLabel("URL:")); panel.add(urlField); panel.add(webButton); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }