Search code examples
javascriptrhinoembeddingexit-code

How do you return an exit code from Rhino when embedding it?


I'm using Java to run simple scripts written in JavaScript using the default Rhino bundled by the JRE. I would like to be able to use the same script within the application and from the command-line version, so I cannot use java.lang.System.exit(3) (it would exit the host application prematurely.) I cannot use the security manager to block it, as people complain about performance issues when a security manager is in effect.

Is there perhaps some function in JavaScript for exiting a script?


Solution

  • Here is an idea:

    Wrap your script in a function and call it. Returning from this function will exit your script.

    //call main
    main();
    //The whole work is done in main
    function main(){
      if(needToExit){
        //log error and return. It will essentially exit the script
        return;
      }
      //your script goes here
    }