从Java程序打开浏览器窗口 [英] Open Browser window from Java program
问题描述
问
我用Java编写的应用程序。它的目的是在Linux机器上运行的独立。我想产生一个新的火狐的窗口。然而,的火狐的永远打开。它始终为1的外壳退出code,我可以运行同样的code用的的gnome-terminal 的,它打开罚款。
背景
所以,这里是它的初始化过程:
- 启动XXorg的:1 -br -terminate -dpms -quiet VT7
- 启动窗口管理器的metacity --display =:1 --replace
- 配置资源xrdb -merge的/ etc / X11 / Xresources中
- 从成为控制终端的后台进程和断开
一旦程序建立一个运行时,有一个按钮,用户可以单击应该产生一个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:
- 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
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屋!