1

Rhino script execution used for executing java script in Java code is not able to identify multiple javascripts.

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine jsEngine = factory.getEngineByName("javascript");    
Reader reader = new FileReader(new File(this.getClass().getClassLoader().getResource("file1.js").toURI()));
    jsEngine.eval(reader);

If file1.js uses jquery-1.3.2.min.js internally for execution how to use import statement for the same.

I am using the following maven dependency for Rhino:

  <!-- Mozilla Rhino JavaScript engine -->
        <dependency>
            <groupId>rhino</groupId>
            <artifactId>js</artifactId>
            <version>1.7R2</version>
        </dependency>

No Idea why it is behaving in such a manner.

1 Answer 1

1

Found the solution :

 ScriptEngineManager factory = new ScriptEngineManager();
          ScriptEngine jsEngine = factory.getEngineByName("javascript");
          jsEngine.put("out", System.out);
          Reader reader1 = null;
          Reader reader2 = null;
          Reader reader3 = null;
        try {
            reader1 = new FileReader(new File(this.getClass().getClassLoader().getResource("file1.js").toURI()));
            reader2 = new FileReader(new File(this.getClass().getClassLoader().getResource("file2.js").toURI()));
            reader3 = new FileReader(new File(this.getClass().getClassLoader().getResource("file3.js").toURI()));

            ScriptableObject scope = context.initStandardObjects();
            context.evaluateReader(scope, reader1, "reader1", 1, null);
            context.evaluateReader(scope, reader2, "reader2", 1, null);
            context.evaluateReader(scope, reader3, "reader3", 1, null);

            Function fct = (Function)scope.get("METHOD_NAME", scope);
            Object params[] = {"",""}
            result = fct.call(context, scope, scope, params);

        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.