java打开浏览器,从Java程序打开浏览器窗口

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:

Start X "Xorg :1 -br -terminate -dpms -quiet vt7"

Start Window Manager "metacity --display=:1 --replace"

Configure resources "xrdb -merge /etc/X11/Xresources"

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

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

Map 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:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值