Share this page 

Start the default browser from an applicationTag(s): IO


In this snippet, we initialize a Listbox from a file containing some URLs. When we double click an item, the default browser is started with the selected HTML page as parameter. This example is Windows oriented since I have used the start command which supports the file association.

[urlList.txt]
http://www.rgagnon.com/javadetails/java-0001.html|JAVA How-to 1
http://www.rgagnon.com/javadetails/java-0002.html|JAVA How-to 2
http://www.rgagnon.com/javadetails/java-0003.html|JAVA How-to 3
http://www.rgagnon.com/javadetails/java-0004.html|JAVA How-to 4
http://www.rgagnon.com/javadetails/java-0005.htmL|JAVA How-to 5
[StartBrowser.java]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class StartBrowser {
  public static void main(String s[]) {
    AFrame f = new AFrame();
    }
 }

 class AFrame extends Frame implements ActionListener {
   List lbx;
   String url[] = new String[50];

   public AFrame() {
     // dispaly setup
     setTitle("URL selection");
     setSize(400,400);
     lbx = new List();
     add(lbx);
     initLbx();
     // action on listbox double click
     lbx.addActionListener(this);
     // to close the Frame
     addWindowListener
       (new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
           System.exit(0);
           }
         }
       );
     setVisible(true);
     }

   public void actionPerformed (ActionEvent ae) {
     String theUrl = url[lbx.getSelectedIndex()];
     // start the default browser (Win95 platform)
     //  on listbox double click

     String cmdLine = "start " + theUrl;
     // on NT, you need to start cmd.exe because start is not
     // an external command but internal, you need to start the
     // command interpreter
     // String cmdLine = "cmd.exe /c " + cmdLine;
     try {
       Process p = Runtime.getRuntime().exec(cmdLine);
       }
     catch (Exception e) {
       e.printStackTrace();
       }
     }

   public void initLbx() {
   int i = 0;
    try {
      String aLine = "";
      BufferedReader in
         = new BufferedReader(new FileReader("urlList.txt"));
      while(null != (aLine = in.readLine())) {
         java.util.StringTokenizer st =
             new java.util.StringTokenizer(aLine, "|");
         url[i++] = st.nextToken();
         lbx.addItem(st.nextToken());
         // lbx.add(st.nextToken());   in JDK1.2
         }
      }
    catch(Exception e) {
      e.printStackTrace();
      }
    }
}
Another way on Windows platform to start the default browser is ;
Runtime.getRuntime().exec
    ("rundll32 url.dll,FileProtocolHandler " + theUrl);

You may have difficulty to open a URL ending with .htm. All you need is to replace the last m with %6D, like

rundll32 url.dll,FileProtocolHandler http://www.rgagnon.com/howto.htm

for

rundll32 url.dll,FileProtocolHandler http://www.rgagnon.com/howto.ht%6D

JDK1.6 has java.awt.Desktop.open(File)

See http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html

JDIC provides the equivalent API for 1.4 and later.

See https://jdic.dev.java.net

try {
   Desktop.browse(new URL("http://www.rgagnon.com");

} 
catch (MalformedURLException e1) {
   e1.printStackTrace();
} 
catch (DesktopException e2) {
   e2.printStackTrace();
}
See also this HowTo.