我正在尝试在Java的默认浏览器中打开一个链接.我不想创建一个简单的Web浏览器,我想使用现有的浏览器.人们建议使用以下内容:
Desktop.getDesktop().browse(new URI("http://www.targetsite.com"));
然而,这只是在调用浏览时挂起,我最终不得不强行退出.
我看到了运行以下内容的建议,以确定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,url,desktop,web
来源: https://codeday.me/bug/20190527/1166476.html