1

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

4

1 回答 1

1

代码失败是因为 Dojo 检测到它在 Rhino 环境中运行的方式。

Dojo 代码是为了与 Rhino 的 shell ( ) 兼容而编写的,org.mozilla.javascript.tools.shell.Main并通过查找由org.mozilla.javascript.tools.shell.Global. 如果这些不存在,Dojo 假定它在浏览器环境中运行。

通过在您的脚本引擎中定义它们来模拟这些功能可能是可能的,但我还没有尝试过。

我写了一篇关于在嵌入式 Rhino 中运行 Dojo 的博客文章,但它不使用ScriptEngineAPI。

于 2013-04-12T12:31:05.070 回答