1

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();
            }
        }
    }
}
}
2

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

That totally did it, except with a slight change, I had to use quotes for the -Command as shownString 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);
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.