REG - 在java中从本地计算机运行远程批处理文件

时间:2016-06-08 11:32:31

标签: java batch-file processbuilder winrm

我已经使用winrs命令从命令提示符执行了批处理文件它执行得很好,当我使用进程构建器执行与java程序相同的命令时,我得到IOException,就像指定的路径不可用 错误2指定的路径不可用

java.io.IOException: Cannot run program "cmd /c C:\Windows\System32\winrs.exe -r:http://ipaddress:port -u:username -p:password dir": CreateProcess error=2, The system cannot find the file specifiedProgram terminated!

at java.lang.ProcessBuilder.start(Unknown Source)
at com.sample.CmdExec.main(CmdExec.java:28)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 2 more

1 个答案:

答案 0 :(得分:0)

ProcessBuilder不使用像

这样的完整命令行
ProcessBuilder b = new ProcessBuilder("cmd /c C:\Windows\System32\winrs.exe -r:http://ipaddress:port -u:username -p:password dir");

但需要将可执行文件和参数分开:

ProcessBuilder b = new ProcessBuilder("cmd", "/c", "C:\Windows\System32\winrs.exe", "-r:http://ipaddress:port", "-u:username", "-p:password", "dir");