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

4.5k Views Asked by At

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)

3

There are 3 best solutions below

0
On BEST 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);
0
On

I know this answer is very late. But I want to do the same and had the same problems - maybe this will help the next one. It took a while to figure out which one of the hundrets of github forks on env-js will do the job. I found out that this combination will work for a simple test:

git clone https://github.com/thatcher/envjs-site.git
#note the different fork!
wget https://raw.github.com/thatcher/env-js/master/src/dom/sizzle.js 
wget http://d3js.org/d3.v3.min.js

java -jar dist/env-js-1.1.jar
load("lib/env.rhino.js");
load("sizzle.js");
load("d3.v3.min.js");
d3.select("body").append("svg").selectAll("line").data([1,2]).enter().append("line").attr("x1", function(d){return d;});
document.innerHTML;

<html><head/><body><line/><line/><svg xmlns="http://www.w3.org/2000/svg"><line x1="1"/><line x1="2"/></svg></body><line/><line/></html>

j

1
On

I did attempt this but couldn't go very far. I also wanted to generate SVGs on server side, with requests being initiated from java(Glassfish in my case). The only way you can do this is with jsdom & Node.js. I am able to do this successfully. Sadly, other than Node.js + jsdom, there seems no other way to do this.

Once you get it working, there are bigger problems lurking if you try to heavily load Node.js with SVG generation requests.