Set and get global variables

suggest change
// Obtain an instance of JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

try {
    // Set value in the global name space of the engine
    engine.put("name", "Nashorn");
    // Execute a hardcoded script
    engine.eval("var value='Hello'+name+'!';");
    // Get value
    String value = (String) engine.get("value");
    System.out.println(value);
} catch (ScriptException ex) {
    // This is the generic Exception subclass for the Scripting API
    ex.printStackTrace();
}

// Outcome:
// 'Hello Nashorn!' printed on standard output

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents