2 回答
TA贡献1951条经验 获得超3个赞
我最终使用的代码执行一些AppleScript代码:(正如DanielPryden所建议的那样)
public static void main(String[] args){
if(args.length == 0 && System.getProperty("os.name").toLowerCase().contains("mac")){
try {
String path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
String command = "tell application \"Terminal\"\n" +
"do script \"java -jar \'" + path + "\' isInConsole\"\n" +
"close the front window\n" + // because "do script..." opens another window
"activate\n" +
"end tell";
String[] arguments = new String[]{"osascript", "-e", command};
Runtime.getRuntime().exec(arguments);
System.exit(0);
} catch (IOException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// program continues...
}
TA贡献1820条经验 获得超9个赞
为了创建进程,类已被 类 所取代。一篇关于类的非常古老但仍然相关的文章(因为它是在将类添加到JDK之前发布的)是 当 runtime.exec() 不会,并且也与类相关。RuntimeProcessBuilderRuntimeProcessBuilderProcessBuilder
如本文所述,方法不是“shell”,因此不会解析您作为单个参数提供给它的命令。您可以通过提供 s 数组来帮助该方法进行解析。exec()StringString
我建议你阅读这篇文章,以及javadoc for class。java.lang.ProcessBuilder
添加回答
举报
