我正在尝试从 Java 在默认浏览器中打开一个链接。我不想创建一个简单的网络浏览器,我想使用现有的浏览器。人们建议使用以下内容:
Desktop.getDesktop().browse(new URI("http://www.targetsite.com"));
但是,这只是在调用 browse
时挂起,我最终不得不强制退出。
我看到了运行以下命令的建议,以确定 Desktop 和 desktop.browse 是否应该在我的系统上运行:
if (Desktop.isDesktopSupported()) {
System.out.println("Desktop IS supported on this platform ");
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
System.out.println("Action BROWSE IS supported on this platform ");
} else {
System.out.println("Action BROWSE ISN'T supported on this platform ");
}
} else {
System.out.println("Desktop ISN'T supported on this platform ");
}
给出了输出:
Desktop IS supported on this platform
Action BROWSE IS supported on this platform
我能做些什么来修复或解决这个问题?
使用 Java 1.8.0_73
最佳答案
看起来你做的一切都正确,你的代码应该在 Windows 和 OS X 中按预期工作。毫不奇怪,Linux 中对 Desktop
的支持不是很好。您可以尝试另一种方法:
if (Runtime.getRuntime().exec(new String[] { "which", "xdg-open" }).getInputStream().read() != -1) {
Runtime.getRuntime().exec(new String[] { "xdg-open", urlString });
}
关于java - 支持桌面和桌面浏览器,但浏览器仍然挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926495/