[ create a new paste ] login | about

Link: http://codepad.org/IDOR4jxY    [ raw code | fork ]

cky - Plain Text, pasted on Jun 21:
import java.io.Console;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

class Jsr223Test {
    public static void main(String[] args) throws ScriptException {
        String lang = args.length > 0 ? args[0] : "js";
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName(lang);
        engine.getBindings(ScriptContext.GLOBAL_SCOPE).put("exit",
                engine.eval("Packages.Jsr223Test$ExitError.exit"));
        Console console = System.console();
        String line;
        while ((line = console.readLine("%s> ", lang)) != null) {
            try {
                engine.eval(line);
            } catch (ScriptException e) {
                e.printStackTrace();
            } catch (ExitError e) {
                System.exit(e.getCode());
            }
        }
    }

    public static class ExitError extends Error {
        private final int code;

        public ExitError(int code) {this.code = code;}
        public int getCode() {return code;}
        public static void exit(int code) {throw new ExitError(code);}
    }
}



Create a new paste based on this one


Comments: