<%bte.doc super="item.bte" %> <%bte.tpl name=pageTitle%>Browser Opener<%/bte.tpl%> <%bte.tpl name=description%>Open a browser from a Java application on Windows, Unix, or Macintosh.<%/bte.tpl%> <%bte.tpl name=keywords%>open url in browser from java, java open url, browser open from java, <%/bte.tpl%> <%bte.tpl name=content%>
Internationalization - included languages: Translations gladly accepted. Please translate Browser.properties.
You may want to use: Attesoro - A Java Translation Editor

Open a browser from a Java application on Windows, Unix, or Macintosh.

Usually the browser chooses if the url should be opened in a new window or not. Internet Explorer on windows opens each URL in the same window while Mozilla opens each in a new window. This can be annoying for developers trying to open a bunch of urls using this class, but have each url in its own window. Workaround methods that use Javascript to try and open urls in new windows have been provided for cases in which all in the same window is not desirable.


Example

// Initialize the class.
// Needs to be done once.
Browser.init();

// Display this page
Browser.displayURL("http://ostermiller.org/utils/Browser.html");

There is also a configuration dialog box (Swing) that can be used in GUI applications that use this class. This allows users to choose their own browser or command line which should be used to open the url. The default command line parsing in Java tokenizes only on spaces. This class uses a custom command line parser that allows quoted strings and escape characters so that the command line to the browser can be specified more easily on a variety of platforms.

Browser Configuration Dialog

The original idea for this class came from a JavaWorld Java Tip. That implementation has some serious problems:

  1. Security - With the JavaWorld example it would be possible to give the class a url to open that would fool the command line parser and possibly allow something other than the browser to be executed. (Any url with white space in it would break the example.) com.Ostermiller.util.Browser manipulates the url and urlencodes characters which might confuse the command line parser.
  2. Out of date - There are a variety of browsers available on Linux. It would be nice to check for something better than Netscape. com.Ostermiller.util.Browser checks for a variety of browsers.
  3. Blocking - If you are using Netscape under Linux, a Java program using this library will wait for Netscape to close before continuing. com.Ostermiller.util.Browser waits for one second to check if it is still running rather than blocking.

Locale specific string for this class are in the com.Ostermiller.util.Browser resource bundle. Translations are welcome and appreciated.

[Download /w Source | Version History | Browse Source | Documentation]

<%/bte.tpl%> <%/bte.doc%>