启动进程时重定向标准错误和标准输入时出现问题
我有一些启动 java 进程的 C# 代码。在一台运行 Windows 7 的机器上它运行良好。在另一个运行 XP 的系统上却没有。这是代码...
mServerProcess = new Process();
mServerProcess.StartInfo.FileName = "java";
mServerProcess.StartInfo.Arguments = "-jar my.jar";
mServerProcess.StartInfo.WorkingDirectory = "C:\\my_server";
mServerProcess.StartInfo.UseShellExecute = false;
mServerProcess.StartInfo.CreateNoWindow = true;
mServerProcess.StartInfo.RedirectStandardOutput = true;
mServerProcess.StartInfo.RedirectStandardError = true;
mServerProcess.StartInfo.RedirectStandardInput = true;
mServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerOutputHandler);
mServerProcess.ErrorDataReceived += new DataReceivedEventHandler(ServerErrorHandler);
mServerProcess.SynchronizingObject = this.console;
// Start the process.
mServerProcess.Start();
// Start the asynchronous read of the sort output stream.
mServerProcess.BeginOutputReadLine();
mServerProcess.BeginErrorReadLine();
在 XP 机器上,我从未收到来自进程标准错误的文本,并且该进程总体上似乎已损坏。我无法从标准输入流向它发送任何内容。
现在......有趣的是,如果我注释掉重定向标准输入的代码,我确实会得到标准错误。但当然,我也需要重定向标准输入。
以前有人见过这个吗?...重定向标准输入和标准错误会导致问题吗?
再说一次...我的 Windows 7 机器上没有这个问题。
谢谢, Buzz
更新:
我正在使用 java Logger 类从我的 java 应用程序输出信息。我认为这个问题具体与Java及其ConsoleHandler类如何处理标准错误有关。这让我抓狂!
I've got some c# code that launches a java process. On one machine running Windows 7 it works fine. On another running XP it doesn't. Here is the code...
mServerProcess = new Process();
mServerProcess.StartInfo.FileName = "java";
mServerProcess.StartInfo.Arguments = "-jar my.jar";
mServerProcess.StartInfo.WorkingDirectory = "C:\\my_server";
mServerProcess.StartInfo.UseShellExecute = false;
mServerProcess.StartInfo.CreateNoWindow = true;
mServerProcess.StartInfo.RedirectStandardOutput = true;
mServerProcess.StartInfo.RedirectStandardError = true;
mServerProcess.StartInfo.RedirectStandardInput = true;
mServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerOutputHandler);
mServerProcess.ErrorDataReceived += new DataReceivedEventHandler(ServerErrorHandler);
mServerProcess.SynchronizingObject = this.console;
// Start the process.
mServerProcess.Start();
// Start the asynchronous read of the sort output stream.
mServerProcess.BeginOutputReadLine();
mServerProcess.BeginErrorReadLine();
On the XP machine I never get the text coming from the process's standard error, and the process in general seems broken. I can't send it anything from the standard input stream.
Now... interestingly, if I comment out the code that redirects standard input, I DO get the standard error. But of course, I NEED to redirect standard input as well.
Has anyone seen this before?... where redirecting both standard input and standard error cause a problem?
And again... I don't have this problem on my windows 7 box.
Thanks,
Buzz
UPDATE:
I'm using the java Logger class to output information from my java app. I think this problem is related to Java specifically and how its ConsoleHandler class deals with standard error. This is driving me nuts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该过程可能正在等待一些输入。
尝试给它一些输入。
The process is probably waiting for some input.
Try giving it some input.
如果您重定向输入和输出,则可能会导致 I/O 死锁。根据缓冲的不同,这在不同的操作系统或不同的输入和输出下可能表现不同。例如,当输入 1k 时,它可能工作正常,但输入 4k 时,它会挂起。请参阅文档并搜索死锁:
If you redirect both input and output you can potentially deadlock the I/O. Depending on the buffering this could behave differently on different operating systems or with different inputs and outputs. For example, it might appear to work fine when with 1k of input but hang with 4k. See the documentation and search for deadlock:
您是否正在调用使用 Jline 的 Java 进程?如果是这样,那么您需要使用以下参数调用它:
Are you invoking a Java process that uses Jline? If so then you need to invoke it with the following parameter: