从 Java 程序打开浏览器窗口

发布于 2024-07-08 06:56:35 字数 935 浏览 11 评论 0原文

问题

我有一个用 Java 编写的应用程序。 它被设计为在 Linux 机器上独立运行。 我正在尝试生成一个新的 firefox 窗口。 但是,firefox 永远不会打开。 它的 shell 退出代码始终为 1。我可以使用 gnome-terminal 运行相同的代码,并且它可以正常打开。

背景

所以,这是它的初始化过程:

  1. Start X "Xorg :1 -br -terminate -dpms -quiet vt7"
  2. Start Window Manager "metacity --display=:1 --replace"
  3. 配置资源" xrdb -merge /etc/X11/Xresources"
  4. 成为守护进程并断开与控制终端的连接

一旦程序开始运行,用户可以单击一个按钮,该按钮应该生成一个 Firefox 窗口。 这是我的代码来做到这一点。 请记住 X 正在显示器上运行: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;
  }
}

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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼眸印温柔 2024-07-15 06:56:35

如果您可以将范围缩小到 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();
            }
        }
    }

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();
            }
        }
    }
戒ㄋ 2024-07-15 06:56:35

使用浏览器启动器

调用起来非常简单,直接去

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

Use BrowserLauncher.

Invoking it is very easy, just go

new BrowserLauncher().openURLinBrowser("http://www.google.com");
摘星┃星的人 2024-07-15 06:56:35

在阅读了各种答案和各种评论(来自提问者)之后,这就是我要做的

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) 尝试从 C/C++/ruby/python 执行此操作(启动 firefox),看看是否成功。

3)如果一切都失败了,我将启动一个shell程序,该shell程序将启动firefox!

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

心碎的声音 2024-07-15 06:56:35

如果您阅读并显示标准输出/错误流,您可能会有更好的运气,这样您就可以捕获 Firefox 可能打印的任何错误消息。

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.

如何视而不见 2024-07-15 06:56:35
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());
}
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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文