gpt4 book ai didi

java - 为什么Java中Runtime.getRuntime().exec()找不到 "chgport.exe"?

转载 作者:行者123 更新时间:2023-11-30 05:56:58 25 4
gpt4 key购买 nike

在Java中,我尝试运行:
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\chgport.exe");
以及
进程 p = Runtime.getRuntime().exec("chgport.exe");

但出现以下异常:

java.io.IOException: Cannot run program "C:\Windows\System32\chgport.exe": CreateProcess error=2, The system cannot find the file specified

我正在使用 NetBeans IDE,它使用管理员凭据运行。

最佳答案

我尝试了你的代码,它工作正常,请像这样尝试:

String[] command = {"chgport"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p = pb.start();

我尝试了 Eclipse 中的两种方法,并且都工作正常您是否有可能没有以管理员权限运行 IDE?
您可以尝试关闭IDE并右键单击以管理员身份运行吗?

    try {
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\mspaint.exe");
p.waitFor();

String[] command = {"mspaint"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p2 = pb.start();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

关于java - 为什么Java中Runtime.getRuntime().exec()找不到 "chgport.exe"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52996822/

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