gpt4 book ai didi

java - 从java打开cmd

转载 作者:行者123 更新时间:2023-12-02 05:03:49 30 4
gpt4 key购买 nike

所以我想用这段代码通过java打开cmd

import java.io.IOException;

public class mainer {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("cmd.exe /c start");
System.out.println("ok");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

但是当我执行它时出现此错误

java.io.IOException: Cannot run program "cmd.exe": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at mainer.main(mainer.java:6)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

有趣的是,它在 2 天前有效,现在却给了我这个奇怪的错误

最佳答案

我认为应该是:

Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start"});

了解有关 start 的更多信息,在命令提示符下键入 help start

关于java - 从java打开cmd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998357/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com