after figuring out yesterday how to configure my Eclipse project to be able to run JS code (if your interested: Build a JS server inside of Java for Google AppEngine), I have the next question related to this topic: I got a JS file and a function within it. I need to run that function inside my Java code and to pass a (Java string) variable in it. My file is very basic, it currently looks like this:
public class Com_feedic_readabilityServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
Context cx = ContextFactory.getGlobal().enterContext();
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_5);
Global global = Main.getGlobal();
global.init(cx);
Main.processSource(cx, "server_js/js_init.js");
}
}
What I need to do now is calling the function run()
within the js_init.js
-file. How do I manage that?
after figuring out yesterday how to configure my Eclipse project to be able to run JS code (if your interested: Build a JS server inside of Java for Google AppEngine), I have the next question related to this topic: I got a JS file and a function within it. I need to run that function inside my Java code and to pass a (Java string) variable in it. My file is very basic, it currently looks like this:
public class Com_feedic_readabilityServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
Context cx = ContextFactory.getGlobal().enterContext();
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_5);
Global global = Main.getGlobal();
global.init(cx);
Main.processSource(cx, "server_js/js_init.js");
}
}
What I need to do now is calling the function run()
within the js_init.js
-file. How do I manage that?
- Are you using the JDK 6 ScriptEngine mechanism? Also, it's not at all clear what that function you posted has to do with, well, anything. – Pointy Commented Aug 6, 2010 at 13:52
- It just shows how I import the .js file. And I'm using an imported Rhino js.jar file, it's not exactly the ScriptEngine-mechanism (but I think it's based uppon it, besides: I got no clue). – fb55 Commented Aug 6, 2010 at 14:05
- Hmm ... well I read that blog post and it seemed unlike anything I've ever done with Rhino. I've always used the JDK ScriptEngine framework and the built-in (somewhat dated) version of Rhino, and that's very easy to use. – Pointy Commented Aug 6, 2010 at 14:23
- I'm not using the technique of that thing, just its basic structure (but I do use Java-files). A solution using ScriptEngine would be fine, too. – fb55 Commented Aug 6, 2010 at 16:05
2 Answers
Reset to default 7You need to pass the value of the parameter via a Binding object, as follows:
package rhinodemo;
import java.util.Date;
import javax.script.*;
public class RhinoDemo {
public static void main(String[] args) throws Exception {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
Bindings bindings = engine.createBindings();
bindings.put("currentTime", new Date());
engine.eval(
"function run(x) { println('x=' + x); }" +
"run(currentTime);", bindings);
}
}
If you want your Java code to invoke a Javascript function called run()
then create a script that (a) defines the run()
function and (b) calls this function, passing a parameter to it. Then, in the Java side, you need to create a Bindings object and set the value of this parameter bindings.put(currentTime, new Date())
.
try this:
Object jsOut = Context.javaToJS(System.out, scope);
ScriptableObject.putProperty(scope, "out", jsOut);
and js file out.println(<text>);