在Java中,我曾尝试运行:Process p = Runtime.getRuntime().exec("C:\Windows\System32\chgport.exe");
以及Process p = Runtime.getRuntime().exec("chgport.exe");
但是得到以下异常:
java.io.io异常:无法运行程序"C:\Windows\System32\chgport.exe":CreateProcess错误=2,系统找不到指定的文件
我正在使用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();
}
}
您可以使用CMD /C
运行它,它"执行字符串指定的命令,然后终止"。
Process p = Runtime.getRuntime().exec("CMD /C chgport.exe");