【问题标题】:“Cannot run program” when using Runtime.exec with spaces in command在命令中使用带有空格的 Runtime.exec 时“无法运行程序”
【发布时间】:2020-05-26 11:24:41
【问题描述】:
cmd=powershell.exe;$passwd=convertto-securestring -AsPlainText -Force -String abc;$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist "xyz",$passwd;$session= new-PSSession -Computer mobilesaucelabs.westeurope.cloudapp.azure.com -Credential $cred -UseSSL;Invoke-Command -Session $session -FilePath C:\FetchMfilesVersion.ps1
Process process = Runtime.getRuntime().exec(cmd);
由于空格而引发错误。
有人可以告诉我如何使用 java 运行上述命令。
提前致谢!
【问题讨论】:
标签:
java
powershell
processbuilder
【解决方案1】:
试试这个:
string[] cmd = {"powershell.exe;$passwd=convertto-securestring", "-AsPlainText", "-Force", "-String", "abc;$cred=new-object", "-typename", "System.Management.Automation.PSCredential", "-argumentlist 'xyz'","$passwd;$session=new-PSSession", "-Computer", "mobilesaucelabs.westeurope.cloudapp.azure.com", "-Credential", "$cred", "-UseSSL;Invoke-Command", "-Session", "$session", "AddCodeWithSpacesSoOn"};
Process process = Runtime.getRuntime().exec(cmd);