1

We are invoking an Eclipse RCP application through shell script. If Eclipse RCP application is exiting abnormally, then the ErrorLevel is not set properly due to which the shell is unable to identify the return code of the Eclipse RCP application.

Any ideas on how to set the %ErroLevel% through Eclipse RCP application ??

Ragu
  • 187
  • 1
  • 11
  • Sry..actually it is not a Java application, but an Eclipse application.. From within Eclipse Application, if I set the System.exit(value). This value is not displayed when I do echo %ERRORLEVEL% – Ragu May 19 '15 at 11:02
  • Look at this: http://stackoverflow.com/questions/6298705/exit-code-of-java-program-running-inside-of-the-eclipse-ide . Notice that `Runtime.getRuntime().exit(5)` is equal to `System.exit(5)`. – Btc Sources May 19 '15 at 11:06
  • @BtcSources: yes, Both System.exit(value) & Runtime.getRuntime().exit(5) are similar and after giving these in an Eclipse RCP appication, I'm still getting 0 as the ERROLEVEL – Ragu May 19 '15 at 11:20
  • Kindly correct me if I'm wrong somewhere..... – Ragu May 19 '15 at 11:20
  • Use a breakpoint to check if your program is ending within this line of code. It would be strange if it does but `ERRORLEVEL` doesn't change. Update the answer with two pics, the breakpoint stoped in runtime in the line, and the `` pic. – Btc Sources May 19 '15 at 11:23
  • HI @BtcSources: I coudn't update the pics as I don't have enough Reputations :( . But the line [ System.exit() ] is been executed and I'm able to view the Debug View – Ragu May 19 '15 at 11:37
  • For Normal Java application it seems to work ( I've verified this ) but not for an ECLIPSE APPLICATION – Ragu May 19 '15 at 11:38
  • @BtcSources: If you provide your mail ID then I can show you these PICS. – Ragu May 19 '15 at 11:39
  • @BtcSources : Any idea on why it mis-behaves with an Eclipse RCP application ? Actually for Java application it is working fine, whereas when comes to an Eclipse RCP application then it is not setting the ERRORLEVEL. Instead it is always setting as 0. – Ragu May 19 '15 at 13:37
  • without seeing the result you're getting it's hard to say. Upload both pics to a server and update the post with their links please. – Btc Sources May 19 '15 at 13:43
  • http://www.4shared.com/account/home.jsp#dir=XmQnTOEc. You can find the attachments here in the above 4shared.com – Ragu May 19 '15 at 13:54
  • man... your pic says the exit value is 10, as you've wrote in the `exit` function. It's working fine! http://dc97.4shared.com/img/uowOsN-Gba/s7/14d6c715038/terminate?async&rand=0.648259157504609 – Btc Sources May 19 '15 at 13:58
  • No it is not so, actually Im trying to invoke the RCP application from within a BAT script. So this errorlevel needs to be communicated b/w these 2 applications. but this is not happening. If I do the same thing with help of a NORMAL JAVA Application then it is working fine. – Ragu May 19 '15 at 14:02

3 Answers3

2

This should be simple: just add System.exit(<your error code>);

Example batch file:

java -classpath "%~dp0dist\StackOverflow3.jar" retvalue.RetValue
echo batch layer: %ErrorLevel%
pause

Example java source:

package retvalue;

public class RetValue {

    public static void main(String[] args) {
        System.out.println("returning with 5"); 
        System.exit(5);
    }
}

console output:

C:\daten\chris\source\netbeans\StackOverflow3>java -classpath "C:\daten\chris\source\netbeans\StackOverflow3\dist\StackOverflow3.jar" retvalue.RetValue
returning with 5

C:\daten\chris\source\netbeans\StackOverflow3>echo batch layer: 5
batch layer: 5

C:\daten\chris\source\netbeans\StackOverflow3>pause
Drücken Sie eine beliebige Taste . . .
chris
  • 1,685
  • 3
  • 18
  • 28
1

You have to set it by using System.Exit(value) when you end your application. So a way would be to control your exceptions which make the program to finish abnormally, and then end the program using it to return the value you want for the errorlevel.

You can return the int value you want using it. As example:

// If I had IOException, then
System.exit(3);

Reference: System.exit

Edit: the problem you have about the number comes with the %errorlevel%value, use ERRORLEVEL instead, like:

if ERRORLEVEL 0 (
  echo All ok
) else (
  echo An error
)

Just notice that ERRORLEVEL compares if %errorlevel% has a value equal or higher than the one you're giving to it.

Why do I say this? I've experienced the same problems than you before, that %errorlevel% seems to update later than when it must. I don't know the reason, but i can tell you that if you check it's value by ERRORLEVEL, you would check that this value is the correct one.

You only have to check your higher values returned before, since ERRORLEVEL checks if the value is equal or higher.

Reference: %errorlevel% is not ERRORLEVEL

Btc Sources
  • 1,912
  • 2
  • 30
  • 58
  • Thanks for your instant reply !!. I tried this but after exitting the program and doing echo %ErrorLEvel% still showing 0. and not the value which I'd set using System.exit(value) – Ragu May 19 '15 at 10:45
  • That's problem of `%errorlevel%`, use `ERRORLEVEL` instead. Let me update my answer to show you how. – Btc Sources May 19 '15 at 10:46
  • Hi.. Can you illustrate me how this can be achived. – Ragu May 19 '15 at 10:56
  • @Ragu answer updated, you only have to check your higher values returned before, since `ERRORLEVEL` checks if the value is equal or higher. – Btc Sources May 19 '15 at 10:57
  • My application is an eclipse applciation and not a Java appliocation. So It is with eclipse If I set the System.exit(value) then the same value is not updated with echo %ERRORLEVEL% – Ragu May 19 '15 at 11:02
0

You can set the error code that is returned by the JVM when the process exists using the System.exit(...) call. For example:

System.exit(10);

This will end the JVM process with exit code 10.

Jesper
  • 202,709
  • 46
  • 318
  • 350