从Java程序打开浏览器窗口 [英] Open Browser window from Java program

查看:510
本文介绍了从Java程序打开浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Java编写的应用程序。它的目的是在Linux机器上运行的独立。我想产生一个新的火狐的窗口。然而,的火狐的永远打开。它始终为1的外壳退出code,我可以运行同样的code用的的gnome-terminal 的,它打开罚款。

背景

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


  1. 启动XXo​​rg的:1 -br -terminate -dpms -quiet VT7

  2. 启动窗口管理器的metacity --display =:1 --replace

  3. 配置资源xrdb -merge的/ etc / X11 / Xresources中

  4. 从成为控制终端的后台进程和断开

一旦程序建立一个运行时,有一个按钮,用户可以单击应该产生一个Firefox窗口。这里是我的code这样做。记住x是显示运行:1。

code

 
公共布尔openBrowser()
{
  尝试{
    流程oProc =调用Runtime.getRuntime()EXEC(在/ usr /斌/火狐--display =:1)。
    INT bExit = oProc.waitFor(); //这始终是1出于某种原因    返回true;  }赶上(例外五){
    oLogger.log(Level.WARNING,打开浏览器,电子);
    返回false;
  }
}


解决方案

在看过各种答案和各种评论(从提问者),这里就是我会做

1)试试这个方法的java
http://java.sun.com/j2se /1.5.0/docs/api/java/lang/ProcessBuilder.html

 的ProcessBuilder PB =新的ProcessBuilder(myCommand,myArg1,myArg2);
地图<字符串,字符串> ENV = pb.environment();
env.put(VAR1,myvalue的);
env.remove(OTHERVAR);
env.put(VAR2,env.get(VAR1)+后缀);
pb.directory(MYDIR);
进程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 ++ /拼音/ Python,看看是否正在取得成功。

3)如果一切都失败了,我会推出一个shell程序和shell程序将启动Firefox !!

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

解决方案

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

这篇关于从Java程序打开浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆