JavaScript ScriptEngine does not work in Google App Engine for Java (GAE / J)

I have a problem when I always get the value 0 returned when I try to use ScriptEngine eval. Using Logger, I was able to determine what NullPointerExceptions were generated. After further inspection, it seems that GAE does not always return a valid script engine (if ever) because it throws an exception when you try to use it.

My code looks like this:

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; } } 

Everything works fine if I run it locally as WebApp (both in Eclipse and Netbeans, as well as Tomcat and Glassfish 4.0).

Some lines that I tried to evaluate:

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

NOTE. 0 or 0.0 refer to other evaluations that were not performed in previous calls. Since this function returns 0 on error.

According to the White List of JRE Classes, the ScriptEngineManager and ScriptEngine classes are allowed. Therefore, I do not understand why it does not work as expected.

Any suggestions?

Thanks in advance,

Randy

0
source share
1 answer

I ran into the same problem. Although classes are white, their functionality seems to be limited in App Engine. The code works fine on your local computer, but does not work when deployed to App Engine, since there are no script engines available (hence the NullPointerException).

Fortunately, you can do the same using the Rhino engine.

Note: this example is based on what was given by Harsha R at fooobar.com/questions/1382616 / ...

Download the Rhino Jar and add js.jar to your class path (you only need js-14.jar if you use Java 1.4).

  /* 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(); } 
0
source

Source: https://habr.com/ru/post/1382618/


All Articles