Dojo not getting loaded into rhino emmbedded in java

笑着哭i 提交于 2019-12-08 03:54:30

问题


I'm trying to render the dojo charts server side. I came across the Rhino and envjs for server side browser emulation. when I tried to an example program to load the dojo.js in the rhino embedded in the java impl, the exception is thrown,

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.EcmaError: ReferenceError: "location" is not defined. (#15) in at line number 15.

My code is as follows:

import javax.script.*;
import java.io.*;

public class Java6RhinoRunner {
  public static void main(String[] args) throws ScriptException {
    new Java6RhinoRunner().load(args[0]);
  }

  private final ScriptEngine engine;

  public Java6RhinoRunner() throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    this.engine = factory.getEngineByName("JavaScript");

    this.engine.put("Java6RhinoRunner", this);
    this.engine.eval("function load(filename) { Java6RhinoRunner.load(filename); }");
  }

  public void load(String filename) throws ScriptException {
    try {
      this.engine.eval(new FileReader(filename));
    }
    catch(FileNotFoundException e) {
      throw new RuntimeException("Error loading javascript file: " + filename, e);
    }
  }
}

Lots of googling has been done but all in vain. Please help me to solve this problem


回答1:


The code fails because of the way Dojo detects that it is running in the Rhino environment.

The Dojo code was written for compatibility with Rhino's shell (org.mozilla.javascript.tools.shell.Main) and detects Rhino by looking for features defined by org.mozilla.javascript.tools.shell.Global. If these are not present Dojo assumes it is running in a browser environment.

It is probably possible to emulate these features by defining them in your script engine but I haven't tried it.

I wrote a blog post on running Dojo in embedded Rhino but it doesn't use the ScriptEngine API.



来源:https://stackoverflow.com/questions/15969392/dojo-not-getting-loaded-into-rhino-emmbedded-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!