我知道这是一个古老的问题,但是有时会Desktop.getDesktop()
产生意外的崩溃,例如在Ubuntu 18.04中。因此,我必须像这样重新编写我的代码:
public static void openURL(String domain)
{
String url = "https://" + domain;
Runtime rt = Runtime.getRuntime();
try {
if (MUtils.isWindows()) {
rt.exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor();
Debug.log("Browser: " + url);
} else if (MUtils.isMac()) {
String[] cmd = {"open", url};
rt.exec(cmd).waitFor();
Debug.log("Browser: " + url);
} else if (MUtils.isUnix()) {
String[] cmd = {"xdg-open", url};
rt.exec(cmd).waitFor();
Debug.log("Browser: " + url);
} else {
try {
throw new IllegalStateException();
} catch (IllegalStateException e1) {
MUtils.alertMessage(Lang.get("desktop.not.supported"), MainPn.getMainPn());
e1.printStackTrace();
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public static boolean isWindows()
{
return OS.contains("win");
}
public static boolean isMac()
{
return OS.contains("mac");
}
public static boolean isUnix()
{
return OS.contains("nix") || OS.contains("nux") || OS.indexOf("aix") > 0;
}
然后,我们可以从实例中调用此帮助器:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MUtils.openURL("www.google.com"); // just what is the 'open' method?
}
});