[英]Java Runtime.exec() not returning PowerShell command output
I'm using Java to invoke a PowerShell command and then capturing the output. 我正在使用Java调用PowerShell命令,然后捕获输出。
I've run into an issue where one simple PowerShell command will return output, but another won't and I'm trying to understand why. 我遇到了一个问题,其中一个简单的PowerShell命令将返回输出,而另一个则不会,并且我试图理解原因。
This is the logic (updated to include a check of the error output stream) 这是逻辑 (已更新为包括对错误输出流的检查)
public class Example {
private void myMethod(String command) throws IOException {
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
String line;
while((line = bufferedReader.readLine()) != null){
output += (line + "\n");
}
System.out.println((output.isEmpty() ? "No output was received!!!" : output));
// Also iterate through the error stream to see if it caught anything.
BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorOutput = "";
while((line = errorBufferedReader.readLine()) != null){
errorOutput += (line + "\n");
}
System.out.println((errorOutput.isEmpty() ? "Nothing in the error output stream." : errorOutput));
}
public static void main(String[] args) throws IOException {
new Example().myMethod("powershell -Command \"$PSVersionTable\"");
new Example().myMethod("powershell -Command \"Get-Module -Name VMware* -ListAvailable\"");
}
}
This is the output 这是输出
Name Value
---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.18444
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Nothing in the error output stream.
No output was received!!!
Nothing in the error output stream.
So the 1st command being executed in Main()
returns output, but the 2nd one doesn't and I don't understand why. 因此,在Main()
执行的第一个命令返回输出,但是第二个命令却没有,我也不明白为什么。
I can run the 2nd command manually through command prompt and it definitely works. 我可以通过命令提示符手动运行第二个命令,它肯定可以工作。
Any ideas on why I can't retrieve the output of the 2nd command when I'm programmatically executing it? 关于为什么我以编程方式执行第二个命令的输出时无法检索到第二个命令的想法?
Update 更新资料
When manually running the 2nd command, this is the output that gets generated.(and what I would expect my Java code to be capturing) 手动运行第二个命令时,这是生成的输出(以及我期望捕获的Java代码)
U:\>powershell -Command "Get-Module -Name VMware* -ListAvailable"
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 6.5.1.6... VMware.DeployAutomation {Add-DeployRule, Add-ProxyServer, Add-ScriptBundle, Copy-DeployRule...}
Binary 6.5.1.6... VMware.ImageBuilder {Add-EsxSoftwareDepot, Add-EsxSoftwarePackage, Compare-EsxImageProfile, Export-EsxImageProfile...}
Manifest 6.5.4.7... VMware.PowerCLI
Binary 6.5.4.6... VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Binary 6.5.1.5... VMware.VimAutomation.Cloud {Add-CIDatastore, Connect-CIServer, Disconnect-CIServer, Get-Catalog...}
Manifest 6.5.4.6... VMware.VimAutomation.Common
Binary 6.5.2.6... VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer...}
Binary 6.5.4.7... VMware.VimAutomation.HA Get-DrmInfo
Binary 7.1.0.5... VMware.VimAutomation.HorizonView {Connect-HVServer, Disconnect-HVServer}
Binary 6.5.1.5... VMware.VimAutomation.License Get-LicenseDataManager
Binary 2.0.0.6... VMware.VimAutomation.Nsxt {Connect-NsxtServer, Disconnect-NsxtServer, Get-NsxtService}
Binary 6.5.1.5... VMware.VimAutomation.PCloud {Connect-PIServer, Disconnect-PIServer, Get-PIComputeInstance, Get-PIDatacenter}
Manifest 1.0.0.5... VMware.VimAutomation.Sdk {Get-PSVersion, Get-InstallPath}
Binary 6.5.1.5... VMware.VimAutomation.Srm {Connect-SrmServer, Disconnect-SrmServer}
Binary 6.5.4.7... VMware.VimAutomation.Storage {Add-KeyManagementServer, Copy-VDisk, Export-SpbmStoragePolicy, Get-KeyManagementServer...}
Script 1.1 VMware.VimAutomation.StorageUtility Update-VmfsDatastore
Binary 6.5.1.5... VMware.VimAutomation.Vds {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, Export-VDPortGroup, Export-VDSwitch...}
Binary 6.5.4.7... VMware.VimAutomation.Vmc {Connect-Vmc, Disconnect-Vmc, Get-VmcService, Connect-VmcServer...}
Binary 6.5.1.5... VMware.VimAutomation.vROps {Connect-OMServer, Disconnect-OMServer, Get-OMAlert, Get-OMAlertDefinition...}
Binary 6.5.1.5... VMware.VumAutomation {Add-EntityBaseline, Copy-Patch, Get-Baseline, Get-Compliance...}
So I figured out what was going on here. 所以我弄清楚了这里发生了什么。
Quick recap of the problem :: 快速回顾问题::
Reason this was happening :: 发生这种情况的原因::
Once I installed the VMware modules via the 32-bit Powershell, then my Java program (which runs in a 32-bit context) began working as expected. 通过32位Powershell安装VMware模块之后,我的Java程序(在32位上下文中运行)开始按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.