简体   繁体   中英

How do set the error code (ErrorLevel) for an Eclipse RCP application

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 ??

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 . . .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM