【发布时间】:2022-11-28 22:59:02
【问题描述】:
我有一个简单的Java程序我想做的是打开一个网址为了例如“https://github.com”在我的操作系统的默认浏览器中,我使用视窗 10.
这是我运行程序时得到的蚀:
我认为有问题我的代码:
package com.main;
import java.awt.Desktop;
import java.net.URI;
public class Browser {
public void displayURL() throws Exception {
String url = "https://github.com";
String myOS = System.getProperty("os.name").toLowerCase();
System.out.println("(Your operating system is: " + myOS + ")\n");
try {
if (Desktop.isDesktopSupported()) {
System.out.println(" -- Going with Desktop.browse ...");
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI(url));
} else {
ProcessBuilder pb = new ProcessBuilder();
if (myOS.contains("windows 10")) {
System.out.println("Hello Windows 10");
pb.command("start " + url);
pb.start();
} else if (myOS.contains("mac")) {
pb.command("open " + url);
pb.start();
} else if (myOS.contains("nix") || myOS.contains("nux")) {
pb.command("xdg-open " + url);
pb.start();
} else {
System.out.println("Sorry!! I could not launch the browser on your operating system.");
}
}
} catch (Exception e) {
System.out.println("Oops!! Something is wrong. " + e.getMessage());
}
}
}
我希望找到解决办法
【问题讨论】:
-
Desktop.isDesktopSupported()返回false。后面的条件不包括myOS是"windows 10"的情况,你需要为此添加代码。 -
你能给我举个例子吗
-
我是 Java 新手
标签: java eclipse command-line-interface java-11