ReferenceError: “alert” is not defined

alert is not part of JavaScript, it’s part of the window object provided by web browsers. So it doesn’t exist in the context you’re trying to use it in. (This is also true of setIntervalsetTimeout, and other timer-related stuff, FYI.)

If you just want to do simple console output, Rhino provides a print function to your script, so you could replace alert with print. Your script also has access to all of the Java classes and such, so for instance java.lang.System.out.println('Hello'); would work from your JavaScript script (although it’s a bit redundant with the provided print function). You can also make Java variables available to your script easily via ScriptEngine.put, e.g:

engine.put("out", System.out);

…and then in your script:

out.println('Hello from JavaScript');

…so that’s a third way to do output from the script. 🙂

See the discussion in the javax.script package documentation, in particular ScriptEngine#put, or for more complex cases, Bindings (and SimpleBindings) and ScriptContext.

Leave a Comment