最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call a (Rhino) JS function in Java and pass a variable in - Stack Overflow

programmeradmin0浏览0评论

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?

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Aug 6, 2010 at 13:40 fb55fb55 1,2271 gold badge12 silver badges16 bronze badges 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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>);

发布评论

评论列表(0)

  1. 暂无评论