Click to See Complete Forum and Search --> : Calling JAR file hangs Process


Wheaties
May 19th, 2009, 11:08 AM
.NET version: 2.0
Platform: Windows XP, SP2
Compiler: VS2005

Problem:

I call a JAR file using a Process by first passing in a ProcessInfo object. The JAR file runs successfully, however, the java.exe process does not terminate even when consuming ~0 CPU cycles. Hence a call to Process::waitForExit() hangs my program. Anyone run into this sort of problem?

Code:



ProcessStartInfo AcSPInfo = new ProcessStartInfo("\"C:\\Program Files\\Java\\jre1.6.0_07\\bin\\java.exe\"",
" -jar C:\\Workspace\\Scratch\\AcSP.jar");

AcSPInfo.WorkingDirectory = "C:\\Workspace\\Scratch";
AcSPInfo.RedirectStandardError = false;
AcSPInfo.RedirectStandardInput = false;
AcSPInfo.RedirectStandardOutput = true;
AcSPInfo.CreateNoWindow = true;
AcSPInfo.UseShellExecute = false;

Process AcSP = new Process();

AcSP.StartInfo = AcSPInfo;

try
{
AcSP.Start();
StreamReader output = AcSP.StandardOutput;
AcSP.WaitForExit(); //la la la, when will you finish!?
Console.Out.Write(output.ReadToEnd());
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
}

AtWork
May 19th, 2009, 11:43 AM
I hate java. It's way too easy to break the Java.exe process. I almost shudder anytime I have to use a web app that uses Java. My only advice is to look at the java log console to see what it might be hanging on.

Wheaties
May 19th, 2009, 12:52 PM
Looks like I found the solution to my problems:

http://cleancode.sourceforge.net/api/csharp/CleanCode.IO.ExecProcess.html

wonderful little bit of code this fellow wrote.

Arjay
May 19th, 2009, 03:26 PM
Another option is to use the WaitForExit( int timeout ) overload. This will wait for the process to finish up to the specified timeout value.