views:

443

answers:

2

How can I obtain the JVM exit code (value of 'status' from call: System.exit(status)) from a Windows program which started this JVM? I tried to use result from the ShellExecute() call, but the result (42) was independent of real value of status.

+1  A: 

The MSDN docs for that function make it quite clear that it does not return the exit codes from the called application.

If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.

There appears to be some example code for getting exit status at MSDN as well.

jsight
+2  A: 

Start the external application by using ShellExecuteEx instead of ShellExecute.

Before calling ShellExecuteEx, enable the SEE_MASK_NOCLOSEPROCESS flag in the parameter to the ShellExecuteEx function. You will then receive a handle to the started process in the hProcess field of the parameter to the ShellExecuteEx function.

ShellExecuteEx: http://msdn.microsoft.com/en-us/library/bb762154(VS.85).aspx

Then, use the WaitForSingleObject function or any other WaitFor* function to wait until the external application is terminated.

WaitForSingleObject: http://msdn.microsoft.com/en-us/library/ms687032.aspx

Then, use the GetExitCodeProcess function to read the exit code of the external process.

GetExitCodeProcess: http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx

NineBerry
Thank You. Eventually I changed the idea of my java program executed from my main program, so now there is no need to obtain the jvm exit code.