为什么可以';";chgport.exe“;是否由Java中的Runtime.getRuntime().exec()找到?
在Java中,我尝试运行:为什么可以';";chgport.exe“;是否由Java中的Runtime.getRuntime().exec()找到?,java,windows,Java,Windows,在Java中,我尝试运行: processp=Runtime.getRuntime().exec(“C:\\Windows\\System32\\chgport.exe”) 以及 processp=Runtime.getRuntime().exec(“chgport.exe”) 但得到以下例外情况: java.io.IOException:无法运行程序“C:\Windows\System32\chgport.exe”:CreateProcess error=2,系统找不到指定的文件 我正在使用N
processp=Runtime.getRuntime().exec(“C:\\Windows\\System32\\chgport.exe”)代码>
以及
processp=Runtime.getRuntime().exec(“chgport.exe”)代码>
但得到以下例外情况:
java.io.IOException:无法运行程序“C:\Windows\System32\chgport.exe”:CreateProcess error=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
运行它,它“执行字符串指定的命令,然后终止”
根据,exec不需要绝对/相对路径,而是command@dnsiv我也尝试过不使用路径,但得到了相同的错误。您尝试过类似的方法吗<代码>字符串[]cmdArray=新字符串[]{“C:\Windows\System32\chgport.exe”};exec(cmdArray)代码>@dnsiv是。使用“mspaint.exe”效果很好。更改为“chgport.exe”,但找不到“chgport.exe”。我的IDE以管理员身份运行,“chgport.exe”可由用户执行。“chgport.exe”文件有一些“东西”(似乎不是权限)。“mspaint.exe”是一个很好的例子,可以帮助我解决这个问题,但我还没有。我的IDE以管理员的身份运行,“chgport”可以由用户执行,所以我不知道。这很奇怪。
Process p = Runtime.getRuntime().exec("CMD /C chgport.exe");