繁体   English   中英

Java Runtime.exec()不返回PowerShell命令输出

[英]Java Runtime.exec() not returning PowerShell command output

我正在使用Java调用PowerShell命令,然后捕获输出。

我遇到了一个问题,其中一个简单的PowerShell命令将返回输出,而另一个则不会,并且我试图理解原因。

这是逻辑 (已更新为包括对错误输出流的检查)

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\"");
   }
}

这是输出

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.

因此,在Main()执行的第一个命令返回输出,但是第二个命令却没有,我也不明白为什么。

我可以通过命令提示符手动运行第二个命令,它肯定可以工作。

关于为什么我以编程方式执行第二个命令的输出时无法检索到第二个命令的想法?

更新资料

手动运行第二个命令时,这是生成的输出(以及我期望捕获的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...}

所以我弄清楚了这里发生了什么。

快速回顾问题::

  • 我已经通过PowerShell库手动安装了一些VMWare模块。
  • 我试图以编程方式检索我已安装的所有可用VMware模块的列表。
  • 我没有任何标准输出或错误输出。

发生这种情况的原因::

  • 我已经通过64位Powershell安装了VMWare模块。
  • 我的Java程序在32位JDK上运行。
  • 因此,当Java打开cmd提示时,它正在打开32位cmd提示,而该提示又调用了32位Powershell。
  • 我没有为32位Powershell安装VMWare模块,这就是为什么当我尝试以编程方式检索所有可用VMWare模块的列表时没有得到任何输出的原因。
  • 当我手动运行命令以检索所有可用VMWare模块的列表时,我使用的是64位Powershell(已为其安装了VMWare模块)

通过32位Powershell安装VMware模块之后,我的Java程序(在32位上下文中运行)开始按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM