从 Java 程序打开浏览器窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/248534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Browser window from Java program
提问by Ryan Ayers
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 firefoxwindow. However, firefoxnever opens. It always has a shell exit code of 1. I can run this same code with gnome-terminaland it opens fine.
我有一个用 Java 编写的应用程序。它旨在独立运行在 Linux 机器上。我正在尝试生成一个新的Firefox窗口。但是,Firefox永远不会打开。它的 shell 退出代码始终为 1。我可以使用gnome-terminal运行相同的代码,并且可以正常打开。
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
- 开始 X "Xorg:1 -br -terminate -dpms -quiet vt7"
- 启动窗口管理器“metacity --display=:1 --replace”
- 配置资源“xrdb -merge /etc/X11/Xresources”
- 成为守护进程并断开与控制终端的连接
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.
一旦程序开始运行,用户可以单击一个按钮,该按钮应该会产生一个 Firefox 窗口。这是我的代码来做到这一点。记住 X 在显示器上运行: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;
}
}
采纳答案by anjanb
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
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();
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
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.
2) 尝试从 C/C++/ruby/python 执行此操作(启动 firefox),看看是否成功。
3) if all else fails, I would launch a shell program and that shell program would launch firefox!!
3)如果所有其他方法都失败了,我将启动一个shell程序,该shell程序将启动firefox!!
回答by James Van Huis
If you can narrow it down to Java 6, you can use the desktop API:
如果您可以将范围缩小到 Java 6,则可以使用桌面 API:
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/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();
}
}
}
回答by Zarkonnen
Use BrowserLauncher.
Invoking it is very easy, just go
调用它很容易,直接去
new BrowserLauncher().openURLinBrowser("http://www.google.com");
回答by kta
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());
}
回答by JesperE
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.
如果您阅读并显示标准输出/错误流,您的运气可能会更好,这样您就可以捕获 firefox 可能打印的任何错误消息。

