Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Lauching browser from java application



We can use new Desktop API in Java SE 6 to lauch default application to open, edit the file.
Please refer:
http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/des...

This help us to launch the html file in the default browser quickly instead of the getting the browser path and lauching it using the RunTime exec()'s method.

Sample Program:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Launch
{
public static void main(String[] args) throws IOException
{
File htmlFile = new File(args[0]);
if (Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
{
Desktop.getDesktop().open(htmlFile);
}
}
}

We were using RunTime exec()'s method to lauching browser from Java application
Following were the ways to get the broser path,

1. Build the browser path manually using getEnv("ProgramFile") + File.Seperator + "Internet Explorer" + File.Seperator + "iexplore.exe";
2. Use "reg query" windows command and query for the browser path and then use the RunTime exec()'s method.
3. cmd /c start in the RunTime exec()'s method
4. rundll32 url.dll,FileProtocolHandler

we can avoid all these if we are using java 1.6 Desktop API :)