Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: "alert" is not defined

I am trying to call a java script function from java code.

Here is my Java code

    public static void main(String[] args) throws FileNotFoundException {
    try {
        /**
         * To call a anonymous function from java script file
         */
        ScriptEngine engine = new ScriptEngineManager()
                .getEngineByName("javascript");
        FileReader fr = new FileReader("src/js/MySpec.js");
        engine.eval(fr);

    } catch (ScriptException scrEx) {
        scrEx.printStackTrace();
    }
}

Here is my java script file:

(function() {
  alert("Hello World !!!");
})();

But when I run main method of driver class it is giving me error as below:

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "alert" is not defined. (<Unknown source>#2) in <Unknown source> at line number 2
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at Java6RhinoRunner.load(Java6RhinoRunner.java:42)
at Java6RhinoRunner.main(Java6RhinoRunner.java:12)

What I know is that it need some script engine to execute it.

For that I added rhino.jar file in to my class path.But this is not working.

I an not getting how to solve this error. Please help.Thanks in advance.

like image 443
Sanjay Jain Avatar asked Jun 29 '12 09:06

Sanjay Jain


People also ask

How do you fix ReferenceError require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

What does ReferenceError mean?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

What does uncaught ReferenceError mean?

The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope.


1 Answers

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 setInterval, setTimeout, 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.

like image 200
T.J. Crowder Avatar answered Oct 14 '22 16:10

T.J. Crowder