0

I want to capture output of a child process spawned from a parent process.

For example, when the parent process runs on a command window like below

c:>parent.exe

it spawns a child process in a separate window.

I tried like below to capture the output (including Java exception), it cannot capture output from child process

c:>parent.exe > error.log

spawned a new process on a new window

Any idea?

Pingpong
  • 7,681
  • 21
  • 83
  • 209

1 Answers1

-1

You can do this very simple with powershell.It's available on every windows-system.

Then you can start you programm with the command start-process documentation Here you can redirect stdout an stderr very easy to a file.

If you want to redirect the stdout to a variable you can run your programm like that.

  1. open cmd
  2. type powershell
  3. $outFromParent = $(parent.exe)

Or if you want to redirect to a file

PS c:> parent.exe > theOutFile.txt

Update

If this not work then you can try the folowing.

PS c:\>$out = & parant.exe 
PS c:\>$out
OR
PS c:\>Write-Host $out

If this not works you can try the Start-Process. See the example. Or look at here

PS C:\> Start-Process -FilePath "parant.exe" -RedirectStandardInput "testin.txt" -RedirectStandardOutput "testout.txt" -RedirectStandardError "testerror.txt"

Note if you start an application that need elevated rights then all of these methods only work if you use an andmistrator powershell.

Hope this helps.

sweting
  • 390
  • 1
  • 8
  • I tried $outFromParent = $(parent.exe), write-host $outFromParent, it returns empty. However, i can see text and java exception displayed on the new window, then the window disappears. Is it possible make the newly created windows not disappear? It also does not work for PS c:> parent.exe > theOutFile.txt. – Pingpong Feb 12 '18 at 23:58
  • I tried all of those, but none of them can capture output of a new process in a new window. – Pingpong Feb 13 '18 at 23:24
  • @Pingpong Do your process have a stdout? Can you post a screen of the window? – sweting Feb 13 '18 at 23:46
  • Both processes run on command console windows. The child console window displays quickly and disappears due to java exception. The parent console window stays. – Pingpong Feb 13 '18 at 23:51
  • @Pingpong What happens if you add in the Start-Process above the option _-NoNewWindow_ and _-Wait_. What's then the content of testout.txt and testerror.txt? – sweting Feb 14 '18 at 00:13
  • Hi, I tried that, but it still does not work. But I captured the output by recording my screen so it works for me. – Pingpong Feb 14 '18 at 22:03