Hen*_*aya 5 java desktop awt shellexecute
这是我的代码
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN)) {
try {
desktop.open(file.getCanonicalFile());
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
System.out.println("Desktop open action is not supported");
}
} else {
System.out.println("Desktop is not supported");
}
Run Code Online (Sandbox Code Playgroud)
我知道安装MagicISO(从这个Java bug中获取)会使这段代码不起作用.但为什么?MagicISO具体做什么?我可以阻止它发生或工作吗?
更糟糕的是,此代码不会抛出任何异常.它只是在那里保持沉默.这真的让我发疯了.
引用来自 Java 的 Launch 文件的第二个答案:
您可以通过以下类启动应用程序Runtime:
在Mac上,
Runtime.getRuntime().exec(new String[] {"open", pathToFile});
Run Code Online (Sandbox Code Playgroud)
在 Windows 上,
Runtime.getRuntime().exec(new String[] {"cmd.exe", "/C", pathToFile});
Run Code Online (Sandbox Code Playgroud)