0

I have a powershell script that is calling a jar via the following code:

Start-Process java -ArgumentList '-jar', "$jarPath", "$csvPath"

However, the output from the jar is not coming through. I'm pretty sure its running successfully, but I'd like to be sure. How can I pass it through to the Powershell console?

chazbot7
  • 598
  • 3
  • 12
  • 34
  • `Start-Process` should do that by default. What does the Java code look like that supposedly generates output? – Ansgar Wiechers Aug 20 '16 at 00:44
  • Maybe this helps? http://stackoverflow.com/questions/8761888/powershell-capturing-standard-out-and-error-with-start-process Worked for me when I had a similar problem – whatever Aug 20 '16 at 11:25
  • @AnsgarWiechers I'm still not seeing anything being written using Start-Process, but I tried just running the `java -jar "$jarPath" "$csvPath"` straight and I'm now getting output to the console. – chazbot7 Aug 22 '16 at 18:47
  • @whatever thank you, but I was looking to get the output as it was being generated, not once at the end. – chazbot7 Aug 22 '16 at 18:48
  • 1
    Try running `Start-Process` with the parameter `-NoNewWindow`. Or simply use the call operator (see below). – Ansgar Wiechers Aug 22 '16 at 19:31
  • @AnsgarWiechers that did it! Thank you. – chazbot7 Aug 22 '16 at 23:33

2 Answers2

3

Replace Start-Process with the call operator:

& java -jar $jarPath $csvPath
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

This works fine for me:

$stdout = "C:\temp\stdout.txt"
Start-Process powershell -ArgumentList "echo 123" -RedirectStandardOutput $stdout -Wait
$output = Get-Content $stdout
echo $output
Remove-Item $stdout

Since I started Powershell process with command echo 123, it returned 123to stdout, so this value is saved to file. Swap Powershell with Java and it should be working as you expect. Remember, that you cannot redirect stdout directly to variable, you must do it via file.

PatrykMilewski
  • 922
  • 8
  • 17
  • I'm looking to hopefully have the output to be written as its generated, rather than once at the end. If that's not possible then this will definitely do the trick! – chazbot7 Aug 22 '16 at 18:17