padmalcom padmalcom - 3 months ago 34
Javascript Question

Envjs and Rhino in a Java application - Where to find env.rhino.js?

I'm trying to run envjs and Rhino in a java application to render SVGs with D3.js.

So far, I can evaluate smaller functions using Rhino but when it comes to setting up envjs, the problems begin. The most important one is that the only tutorial for envjs talks about a file called env.rhino.js. But I have no idea where to find it.

Can anybody help me out?

(Yes, google shows some results but they are not officially belonging to Rhino or envjs)

Answer

First, download env.rhino.js.

Then, use this Java code to start a Rhino instance and load Env.js:

import org.mozilla.javascript.Context;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.Main;

Context cx = Context.enter();
Global scope = new Global(cx);
cx.setOptimizationLevel(-1); 
cx.setLanguageVersion(Context.VERSION_1_5);

Now you can load and run a JavaScript file (using its absolute file-system path)

Main.processFile(cx, scope, ABSOLUTE_PATH_TO_SOME_JAVASCRIPT_FILE);

And/or evaluate JavaScript code and get its String result

(String)cx.evaluateString(scope, "alert('Its WORKING!')", "js", 1, null);