java - 将Runtime.exec与命令中的空格一起使用时的“Cannot run program”

标签 java powershell processbuilder

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运行上述命令。

提前致谢!

最佳答案

试试这个:

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);

关于java - 将Runtime.exec与命令中的空格一起使用时的“Cannot run program”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60162328/

相关文章:

java - ConnectionPool 从 servletcontext 抛出空指针

powershell - -Contains 什么时候不起作用和/或我应该使用其他东西

Java 进程生成器 : command not found

java - 删除字母之间的空格和单词之间的双空格

java - Springs MVC JSON 响应并将其转换为 JS 对象

java - 如何解决 java.lang.NoClassDefFoundError : javax/xml/bind/JAXBException

powershell - 无法在 PowerShell 中将 curl 转换为 Invoke-WebRequest(--不安全/-k 未找到)

sharepoint - 获取网络分析

batch-file - gradle:执行任务 "type:Exec",其中包含许多带空格的参数

java - 如何模拟 Final 类并进行代码覆盖