1

We are trying to call a Powershell script via Java but it hangs when we try to read the output of that script. It hangs on "input.readLine()"

Here is a code we have tried:

        String command = "cmd /c powershell C:\\_checkouts\\TestPowerShell\\passwordExpirationTime.ps1";
        Process p = Runtime.getRuntime().exec(command);
        new InputStreamReader(p.getInputStream());

        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

        StringBuilder stringBuilder = new StringBuilder();;
        for (String line = input.readLine(); line != null; line = input.readLine()) {
            stringBuilder.append(line);
        }

        input.close();

        String msg = stringBuilder.toString();

        System.out.println("msg: " + msg);

We tried looking at this solution Java program hangs when trying to invoke powershell script but none of those suggestions worked for us.

We also tried it without cmd /c and with cmd /k. We really want this as a generic class where any script could be called. i.e. BAT, BASH, SH, Powershell, etc.

Any ideas?

Thanks to the answer below here is the code that worked:

    try{
        final ProcessBuilder pb = new ProcessBuilder("powershell","C:\\psFile.ps1");
        pb.redirectInput(Redirect.from(new File("NUL")));
        final Process p = pb.start();
        final int retcode = p.waitFor();

        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }catch (Exception e){
        e.printStackTrace();
    }

Here's some additional information. Our powershell was returning an error because it was restricted. That is why we needed all the Redirect to NUL. Once we fixed that and it wasn't returning an error we were able to simplify our code to this:

    try{
        final ProcessBuilder pb;

        pb = new ProcessBuilder(args);

        final Process p = pb.start();

        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }catch (Exception e){
        e.printStackTrace();
    }
0

1 Answer 1

1

Use a ProcessBuilder. Any process implying a modicum of I/O should outright refuse to run via Runtime.exec(), alas, it cannot detect that. And Oracle hasn't flagged it as @Deprecated which is a pity.

Anyway:

final File logFile = new File("/path/to/logfile");
final ProcessBuilder pb = new ProcessBuilder("powershell", "/path/to/the/ps1");
pb.redirectOutput(Redirect.to(logFile));
pb.redirectErrorStream(true);

final Process p = pb.start();
final int retcode = p.waitFor();

// deal with retcode
// read result from the log file
Sign up to request clarification or add additional context in comments.

6 Comments

I tried to do this but it hangs on the p.waitFor(); If I comment that out and wait or pause with a debugger then continue it will work. But if I use the waitFor() it's like the process never finishes so it won't continue.
Try and add pb.redirectInput(Redirect.from(new File("NUL"))
Ok that worked! It looks like it needed to send the input somewhere. Are there any concerns using this cross platform on Linux and OSX? Any ideas on how to make it so it doesn't have to write to a temporary filew but could all be done in memory?
That is possible, but more tricky to do. As to NUL that is Windows-specific; you'll need to detect what this is on other platforms; generally this is /dev/null on all Unix systems.
Ok I think I've got it...Thanks a lot.
|

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.