【问题标题】:JavaScript ScriptEngine isn't working within Google App Engine for Java (GAE/J)JavaScript ScriptEngine 在 Google App Engine for Java (GAE/J) 中不起作用
【发布时间】:2014-04-20 23:39:51
【问题描述】:

我遇到了一个问题,当我尝试使用 ScriptEngine eval 时,总是返回 0 值。通过使用 Logger,我能够确定正在生成 NullPointerExceptions。经过进一步检查,GAE 似乎并不总是返回有效的脚本引擎(如果有的话),因为当您尝试使用它时它会引发异常。

我的代码如下:

public double myEval(String JsFormulaStr ) {
    double solutionValue = 0;
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine eng = mgr.getEngineByName("JavaScript");
    if(eng == null) {  // Added this block of code to prevent java.lang.NullPointerException...
        log.severe("Unable to get Script Engine." );
        return 0;
    }
    try {
        Object jsResults = eng.eval(JsFormulaStr);
        solutionValue = Double.parseDouble(jsResults.toString());
        return solutionValue;
    } catch(Exception e) {
        log.severe("[ERROR] in getCalculatedSolution_FromJS_ToDouble()::\n\t" +
                "Formula String is: " + JsFormulaStr + "\n\t" + e);
        return 0;
    }     
}

如果我将它作为 Web 应用程序在本地运行(在 Eclipse 和 Netbeans 中,以及在 Tomcat 和 Glassfish 4.0 中),一切正常。

我尝试评估的一些字符串:

  • 62.0 / 100
  • 0.0 * 352.0
  • (0 - 428) * 1000
  • (0 - 597) * 1000
  • 73.0 / 100

注意:0 或 0.0 来自其他在先前调用中失败的评估。由于此函数在出错时返回 0。

根据Google's JRE Class Whitelist,允许使用 ScriptEngineManager 和 ScriptEngine 类。所以我不明白为什么它没有按预期工作。

有什么建议吗?

提前致谢,

兰迪

【问题讨论】:

    标签: java google-app-engine nullpointerexception scriptengine


    【解决方案1】:

    我遇到了同样的问题。尽管这些类已列入白名单,但它们的功能似乎在 App Engine 上受到限制。该代码在您的本地计算机上运行良好,但在部署到 App Engine 时失败,因为没有任何脚本引擎可用(因此出现 NullPointerException)。

    幸运的是,您可以使用 Rhino 引擎做同样的事情。

    注意:此示例基于 Harsha R 在 https://stackoverflow.com/a/19828128/578821 中给出的示例

    下载 Rhino Jar 并将 js.jar 添加到您的类路径(如果您使用 Java 1.4,则只需要 js-14.jar)。

        /* Example 1: Running a JavaScript function (taken from examples) */
        String script = "function abc(x,y) {return x+y;}";
        Context context = Context.enter();
        try {
            ScriptableObject scope = context.initStandardObjects();
            Scriptable that = context.newObject(scope);
            Function fct = context.compileFunction(scope, script, "script", 1, null);
            Object result = fct.call(context, scope, that, new Object[] { 2, 3 });
            System.out.println(Context.jsToJava(result, int.class));
        } 
        finally {
            Context.exit();
        }
    
        /* Example 2: execute a JavaScript statement */
        script = "3 + 2 * (4*5)";
        context = Context.enter();
    
        try{
            Scriptable scope = context.initStandardObjects();
            Object result = context.evaluateString(scope, script, "<cmd>", 1, null);
            System.out.println(result);
        }
        finally{
            Context.exit();
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 2016-01-03
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多