简体   繁体   中英

Java Runtime.exec() not returning PowerShell command output

I'm using Java to invoke a PowerShell command and then capturing the output.

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.

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.

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)

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 ::

  • I had manually installed some VMWare modules via the PowerShell gallery.
  • I was attempting to programmatically retrieve a list of all of the available VMware modules that I had installed.
  • I was getting no standard output or error output.

Reason this was happening ::

  • I had installed the VMWare modules via a 64-bit Powershell.
  • My Java program was running against a 32-bit JDK.
  • So when Java opened a cmd prompt, it was opening the 32-bit cmd prompt, which in-turn invoked the 32-bit Powershell.
  • I didn't install the VMWare modules for the 32-bit Powershell and that's why I wasn't getting any output when I tried to programmatically retrieve a list of all of the available VMWare modules.
  • When I was manually running the command to retrieve a list of all of the Available VMWare modules, I was using the 64-bit Powershell (which I had installed the VMWare modules for)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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