Nikhil Das Nomula
Nikhil Das Nomula

Reputation: 1953

Java Powershell CreateProcess error=2, The system cannot find the file specified

I am executing powershell commands in java and I have written two programs, however the strange part is one works fine and the other throws the error. The code that throws the error is as shown

I have tried the following 1) Spcifying the fully specified path of powershell 2) My path variable has the following - "C:\WINDOWS\system32\WindowsPowerShell\v1.0"

I know I might be doing something trivial but its been a day and I am unable to figure out what the issue might be

import java.io.IOException;

public class FileCount {

public static void main(String[] args) {
    Process flCntProcess = null;
    try {

        String test  = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe  -Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object }\"";
        System.out.println("Powershell command : " + test);
        ProcessBuilder builder = new ProcessBuilder(test);
        builder.redirectErrorStream(true);
        flCntProcess = builder.start();

        //  FILE COUNT OUTPUT STREAM PROCESSING
        NotifyThreadComplete outputThread = new ProcessHandler(flCntProcess.getInputStream(),"OUTPUT");
        outputThread.addListener(new ThreadCompleteListener() {

            @Override
            public void notifyCompletion(Thread t, long startTm, boolean didErrorOut, String noOfLines) {
                System.out.println("Completed Output Stream Processing");
                System.out.println("Printing values");
                System.out.println("No of Lines : " + noOfLines);
                System.out.println("Did Error out : " + didErrorOut);

                if(didErrorOut) {
                    System.out.println("Do not continue with processing");
                } else {
                    System.out.println("Continue with processing");
                }
            }
        });
        System.out.println("Starting output thread ");
        outputThread.start();

    } catch (Exception e) {
        System.err.println("Exception while counting files using Powershell Command" + e.getMessage());
    } finally {
        if(flCntProcess != null && flCntProcess.getOutputStream() != null) {
            try {
                flCntProcess.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

Upvotes: 1

Views: 7175

Answers (2)

Keith Hill
Keith Hill

Reputation: 201892

Error code indicates the file to execute can't be found. Try splitting up the program from its arguments:

String ps  = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe";
String args  = "-Command \"& { Get-ChildItem C:\\test -Recurse -force | Measure-Object}\"";        
ProcessBuilder builder = new ProcessBuilder(ps, args);

Upvotes: 1

Pyranja
Pyranja

Reputation: 3599

The constructor of ProcessBuilder does not accept a single String containing a cli invocation, but an array of Strings containing in order :

  • the program to be executed
  • its arguments

See the javadoc

So it interprets your whole String test as the program name, splitting it up should work :

final String psh = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
final String args = "-Command & { Get-ChildItem C:\\temp -Recurse -force | Measure-Object }";
final ProcessBuilder builder = new ProcessBuilder(psh, args);

Upvotes: 0

Related Questions