Open a browser window from a Java program

Question

I have an application written in Java. It is designed to work offline on a Linux box. I am trying to create a new firefox window. However, firefox never opens. It always has a shell exit code of 1. I can run the same code using gnome-terminal, and it opens perfectly.

Background

So here is his initialization process:

  • Start X "Xorg: 1 -br -terminate -dpms -quiet vt7"
  • Window Launch Manager "metacity --display =: 1 --replace"
  • Configuring xrdb -merge / etc / X11 / Xresources resources
  • Become a demon and disconnect from the control terminal

As soon as the program starts, a button will appear that the user can click to display the firefox window. Here is my code for this. Remember that X is on the 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;
  }
}
+5
5

( ),

1) java- 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();

:

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

2) ( firefox) C/++/ruby ​​/python , .

3), , , firefox!!

+2

Java 6, API- :

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/

:

    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();
            }
        }
    }
+9

BrowserLauncher.

,

new BrowserLauncher().openURLinBrowser("http://www.google.com");
+4

, /, , firefox.

0
try {
     String url = "http://www.google.com";
     java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
} catch (java.io.IOException e) {
     System.out.println(e.getMessage());
}
0

All Articles