我想捕获从父进程生成的子进程的输出。
例如,当父进程在如下命令窗口上运行时
c:>parent.exe
它在一个单独的窗口中生成一个子进程。
我尝试下面捕获输出(包括Java异常),它无法捕获子进程的输出
c:>parent.exe > error.log
在新窗口上产生了一个新进程
有什么想法吗?
答案 0 :(得分:0)
使用powershell可以非常简单。每个Windows系统都可以使用它。
然后你可以使用命令start-process documentation启动你的程序 在这里,您可以将stdout stderr重定向到文件非常容易。
如果要将stdout重定向到变量,可以像这样运行程序。
powershell
$outFromParent = $(parent.exe)
或者如果您想重定向到文件
PS c:> parent.exe > theOutFile.txt
<强>更新强>
如果这不起作用,那么你可以尝试下面的内容。
PS c:\>$out = & parant.exe
PS c:\>$out
OR
PS c:\>Write-Host $out
如果这不起作用,您可以尝试启动过程。查看示例。 或者查看here
PS C:\> Start-Process -FilePath "parant.exe" -RedirectStandardInput "testin.txt" -RedirectStandardOutput "testout.txt" -RedirectStandardError "testerror.txt"
注意如果启动需要提升权限的应用程序,则只有在使用andmistrator powershell时,所有这些方法才有效。
希望这会有所帮助。