views:

254

answers:

2

I have a C# app that runs a jar file and uses the System.exit code as a return value. A console window is displayed as a part of this jar, and if the user closes it (thereby terminating the jar) it returns an exit code of 143. The problem is that 143 (or any positive integer) could be a valid exit code if the jar completes successfully.

I need a way to manually set the System.exit code to a negative integer. Treating 143 as an exception in the C# app is out of the question.

+1  A: 

Acording to the java documentation you can use System.exit(int n), so just put a negative number as a parameter, also by convention, a nonzero status code indicates abnormal termination.

Diego Dias
I am already able to return the proper codes using System.exit() if the jar completes, but if the user terminates the jar prematurely, I have no control over what is returned.
stickynips
So your problem is another one ...You need to identify a "close or exit" event on your application and throw an exception that could use system.exit
Diego Dias
+2  A: 

As in Diego Dias answer,

The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

If you really want to ignore that, and both the Java and the C# apps are in your hand, an easy workaround is to add 1000 to your System.exit return value in Java, when the jar completes.

Your C# application will recognize the successful execution by a return code >= 1000 and subtract it again. 143 is below 1000 and thus an error.

Peter Lang
Since I own both the Java and C# code, this is a trivial fix to implement. Goes to show that sometimes the simplest solution is the best.
stickynips