Open Browser window from Java program


I have an application written in Java. It is designed to run on a Linux box standalone. I am trying to spawn a new firefox window. However, firefox never opens. It always has a shell exit code of 1. I can run this same code with gnome-terminal and it opens fine.


So, here is its initialization process:

  1. Start X "Xorg :1 -br -terminate -dpms -quiet vt7"
  2. Start Window Manager "metacity --display=:1 --replace"
  3. Configure resources "xrdb -merge /etc/X11/Xresources"
  4. Become a daemon and disconnect from controlling terminal

Once the program is up an running, there is a button the user can click that should spawn a firefox window. Here is my code to do that. Remember X is running on display :1.


public boolean openBrowser()
  try {
    Process oProc = Runtime.getRuntime().exec( "/usr/bin/firefox --display=:1" );
    int bExit = oProc.waitFor();  // This is always 1 for some reason

    return true;

  } catch ( Exception e ) {
    oLogger.log( Level.WARNING, "Open Browser", e );
    return false;

Asked by: Daryl598 | Posted: 21-01-2022

Answer 1

If you can narrow it down to Java 6, you can use the desktop API:

Should look something like:

    if (Desktop.isDesktopSupported()) {
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.BROWSE)) {
            try {
                desktop.browse(new URI("http://localhost"));
            catch(IOException ioe) {
            catch(URISyntaxException use) {

Answered by: Ryan604 | Posted: 22-02-2022

Answer 2

Use BrowserLauncher.

Invoking it is very easy, just go

new BrowserLauncher().openURLinBrowser("");

Answered by: Dominik490 | Posted: 22-02-2022

Answer 3

after having read the various answers and various comments(from questioner), here's what I would do

1) try this java approach

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.put("VAR2", env.get("VAR1") + "suffix");"myDir");
Process p = pb.start();

see more about this class:

2) try doing this(launching firefox) from C/C++/ruby/python and see if that is succeeding.

3) if all else fails, I would launch a shell program and that shell program would launch firefox!!

Answered by: Roland624 | Posted: 22-02-2022

Answer 4

You might have better luck if you read and display the standard output/error streams, so you can catch any error message firefox may print.

Answered by: Julia762 | Posted: 22-02-2022

Answer 5

try {
     String url = "";
} catch ( e) {

Answered by: John192 | Posted: 22-02-2022

Similar questions

java program to open a web page in browser and post some data into the opened page

I m stuck into a problem. The problem is that i want to write a java code which opens a web page in a default browser and post my data into that opened web page. Please could anyone guide me to do this. I dont have a clue in this. Your help is much appreciated. Thanks in advance

browser - How do I turn off a program with a java code?

Hi guys I open in my java application I open a browser page with this code: String URL = ""; try { java.awt.Desktop.getDesktop().browse(; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Now I have to turn off the browser, Is there a code to do that?

How can I run the html file in browser using java program?

This is my code. I am able to open browser but it will not load the html source. class Browser { public static void main(String[]args) { try { Runtime rtime = Runtime.getRuntime(); String url = "‪C:/Program Files (x86)/Internet Explorer/DD.html"; String brow = "C:/Program Files (x86)/Internet Explorer/iexplore.exe"; Process pc = rtime.exec(br...

Java based Swing Browser should support JavaScript

In my company, I am implementing a java based html browser. I found a lot of tools to generate complete browsers only in Swing which are looking like Mozilla. But I was not able to find a browser which supports JavaScript. The browser I will implement should execute JavaScript inside the HTML sides. Do you know of a tool that supports this? Or do you know of a parser which pairs the javascript to something I can us...

How do I sign a Java applet for use in a browser?

I'm trying to deploy a Java applet on my website. I also need to sign it, because I need to access the clipboard. I've followed all the signing tutorials I could find but have not had any success. Here is what I've done so far: Wrote an applet in NetBeans. It runs fine in the applet viewer. Made a .jar file out of it. Created a certificate by doing this:

java - Open new browser window on click of a form button

I want to open a new browser and load a page from the file system (which will be created on clicking that button). My app is a java servlet. Basically I am allowing users to change some HTML on their website. On clicking the button the user get's to see a preview page, that shows what the page looks like with the changes made. What would be the easiest way to do this and what issues can you see, and possibly how we...

java - I want to make a web browser, but I'm not sure where to start?

I want to use WebKit as the layout/rendering engine, and I want to code it in Java. I am having troubles finding any useful info. I saw this question here which cleared things for me slightly, but I need more to get star...

java - Cant open a doc file from the browser

I have a link using tag and it is linked to a .doc file in the server. When I click on the link, instead of giving the open, save box, it opens the file in the browser in the binary format. Has anyone encountered this problem? I am using a Weblogic server.

java - Can I edit an excel file from a browser using POI?

Can I edit an excel file from a browser using POI? Something like google docs maybe? But in java? I've used POI for display only, way way back, but I dont remember it being editable from the browser. Any suggestions of the best approach to this? The excel will display a list/table with data, and each row will either be reported as having 'good' or 'bad' data, and the user can then go to a bad row to correct it and edit th...

java - What is the maximum length a URL can be to be opened in a J2ME browser?

What is the maximum length URL? This is may be handset dependent; on a desktop, it is definitely browser dependent, as discussed in this question. I am interested in specifications or references as much as empirical data. My particular usecase is passing a very long (about 1600 character) URL to a

I want to use Robot class in java applet for web browser to move and click mouse

I have created this applet, It moves mouse to 1000 pos on screen. It works as application but it does not work in applet. I have created signed applet but still it wont move mouse. What should I do to make my Robot class work from browser? My code is as below: import java.applet.Applet; import java.awt.Graphics; import java.awt.Robot; import java.awt.AWTException; public class s extends Applet { pu...

java - Midlet v/s Phone Browser app pros and cons?

How do you weight the pros and cons between a Midlet and Browser based client/server app for a phone ... What are the pros and cons of both... And based on your experience what will you prefer to go in for and why? I have listed some points based on which we can compare both feel free to add your own... Rapid development. Rich UI. Mild data transfer between client and ser...

browser - How can IE lose ability to execute Java applets?

I have an applet which is used on our company intranet. From time to time there are users who are suddenly no longer able to use the applet and they get an error message. I notice that the browser (IE6) no longer displays the Tools->Open Java Console menu. Java 1.5 is still installed and I can open the Java settings dialog from the Control Panel. It just looks like the browser has lost the connection to Java. Reins...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)