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() 不會,并且也與類相關。Runtime
ProcessBuilder
Runtime
ProcessBuilder
ProcessBuilder
如本文所述,方法不是“shell”,因此不會解析您作為單個參數提供給它的命令。您可以通過提供 s 數組來幫助該方法進行解析。exec()
String
String
我建議你閱讀這篇文章,以及javadoc for class。java.lang.ProcessBuilder
添加回答
舉報