6

I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:

            String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";

The command is then executed using:

Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);

At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :

powershell -noprofile -noninteractive "&C:\new\tst.ps1"

which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.

To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.

I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.

Any hints/tips are appreciated :D

Alan

2 Answers 2

9

You have to close OutputStream in order for Powershell to exit.

Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);

proc.getOutputStream().close();
Sign up to request clarification or add additional context in comments.

Comments

2

Is your external program writing to the standard outputs (err and out)? If yes, it can hang waiting for you to consume them from the java parent process. You can get those as InputStreams by calling

Process.getInputStream()

and

Process.getErrorStream()

There's more details here: Javadoc for Process

3 Comments

Thanks for the reply, Im using the Process.getInputStream() call. The ps1 file that I have set up at the moment till i get this java class working is literally calling a mkdir command, just to allow me to easily check its ran, the only output from it should be the standard stuff that powershell splurts out if you use it to create a folder. The odd thing is I see the output fine if I call the powershell command to mkdir from java, but when java calls the ps1 file instead of a command directly it hangs? Any other ideas would be great, as I hope its something really simple that I have overlooked!
I had an issue with calling PS1 scripts over SSH. They would regularly hang in certain cases. The script would run to completion, but not return. It turned out that something was waiting for stdin (I don't know why). So I just tied stdin to nul.
Thanks for your reply Jason. When you say you tied stdin to nul, was this by getting the inputstream of the proc and closing it? I've tried closing the input and output stream of the proc but with no luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.