从 Java 程序打开浏览器窗口
问题
我有一个用 Java 编写的应用程序。 它被设计为在 Linux 机器上独立运行。 我正在尝试生成一个新的 firefox 窗口。 但是,firefox 永远不会打开。 它的 shell 退出代码始终为 1。我可以使用 gnome-terminal 运行相同的代码,并且它可以正常打开。
背景
所以,这是它的初始化过程:
- Start X "Xorg :1 -br -terminate -dpms -quiet vt7"
- Start Window Manager "metacity --display=:1 --replace"
- 配置资源" xrdb -merge /etc/X11/Xresources"
- 成为守护进程并断开与控制终端的连接
一旦程序开始运行,用户可以单击一个按钮,该按钮应该生成一个 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:
- 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您可以将范围缩小到 Java 6,则可以使用桌面 API:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
应该类似于:
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:
使用浏览器启动器。
调用起来非常简单,直接去
Use BrowserLauncher.
Invoking it is very easy, just go
在阅读了各种答案和各种评论(来自提问者)之后,这就是我要做的
1)尝试这种java方法
http://java.sun.com/ j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html
查看有关此类的更多信息:
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
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!!
如果您阅读并显示标准输出/错误流,您可能会有更好的运气,这样您就可以捕获 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.