views:

2847

answers:

4

Question

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.

Background

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.

Code


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;
  }
}
+4  A: 

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

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/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) {
                ioe.printStackTrace();
            }
            catch(URISyntaxException use) {
                use.printStackTrace();
            }
        }
    }
James Van Huis
I tried using this, but I get a traceback.<pre><code>java.io.IOException: Failed to show URI: at sun.awt.X11.XDesktopPeer.launch(XDesktopPeer.java:75) at sun.awt.X11.XDesktopPeer.browse(XDesktopPeer.java:64) at java.awt.Desktop.browse(Desktop.java:368)</code></pre>
Ryan Ayers
A: 

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.

JesperE
I cannot seem to get any output from firefox.
Ryan Ayers
+2  A: 

Use BrowserLauncher.

Invoking it is very easy, just go

new BrowserLauncher().openURLinBrowser("http://www.google.com");
Zarkonnen
+1  A: 

hi there,

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

1) try this java approach http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory("myDir");
 Process p = pb.start();

see more about this class : http://java.sun.com/developer/JDCTechTips/2005/tt0727.html#2
http://www.javabeat.net/tips/8-using-the-new-process-builder-class.html

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!!

anjanb
This worked. As it turns out, since I am not running a Desktop environment, I had to set the HOME environment variable for firefox to run properly.
Ryan Ayers