Question

I'm trying to call a javascript from my java class. Please find below my java code:

    File file = new File(".....js");
ScriptEngineManager factory = new ScriptEngineManager();
 // create a JavaScript engine
                                    ScriptEngine engine = factory.getEngineByExtension("js");
                                    engine.put("engine", engine);
                                    // evaluate JavaScript code from String
                                    try{
                                        engine.eval(new java.io.FileReader(file));
                                    } catch (FileNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();

                                    } catch (ScriptException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

My javascript contains a dollar sign '$' ... So, I'm getting this error once I run my program:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "$" is not defined. (<Unknown source>#4) in <Unknown source> at line number 4

Is '$' not defined as part of the javascript code? Does the Script engine don't recognize it? Thanks for any help.

This is my updated script:

    <script type="text/javascript" src="scripts/jquery.js"></script>;
<script type="text/javascript" src="scripts/jquery-ui.js"></script>;
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js">

$.ajax({
                              type: 'POST',
                              url: "../ManageChannel",
                              data: "_country="+country+"&_featured="+featured+"&_webviewLink="+webviewLink+"&_name="+locationName,
                             success:function(data)
                             {
                                 if (data==2)

                                alert("RSS link is required");

                            else{
                                alert("Channel is successfully created!");
                                filePath=[];
                                window.location.href=window.location.href;


                            }

                                }});
Was it helpful?

Solution

Your example javascript looks like jquery code. Maybe you just need to import the jquery library ...

For how to deal with XMLHttpRequest in Rhino, see this Q&A: XMLHttpRequest in Rhino?.

As Joachim points out, jquery, the DOM APIs and XMLHttpRequest are not part of the ECMAScript specification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top