1

I used this code in java and worked well

    public String execute(String s)
{
    Object result = null;

    ScriptEngineManager manager = new ScriptEngineManager();


    ScriptEngine engine = manager.getEngineByName("JavaScript");


    if (engine == null) {
        throw new UnsupportedOperationException("JavaScript scripting engine not found");
    }

    Log.i("s",s);

    try {
         result = engine.eval(s);
    } catch (Exception  e) {
        Log.i("e",e.toString());
    }
    return result.toString();
}

but when used in android studio with the same input (a) engine.eval(a); returns null!

Log cat of project

pmf
  • 35
  • 9

1 Answers1

0

On Android-Studio you are building your project with the Android framework. The JSR223 is not embedded in Android's implementation of Java by default.

But you can register them with a library.

For example with https://github.com/APISENSE/rhino-android which is embedding rhino, the javascript engine from Java 7 into Android. (disclaimer: I maintain this library)


Edit ensuing your comments

I tried to reproduce your behavior, here is what I have:

When using rhino as ScriptEngine name:

09-20 11:13:16.151 5997-5997/? I/TEST: using Script engine name: rhino
09-20 11:13:16.159 5997-5997/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: test.app, PID: 5997
java.lang.NoClassDefFoundError: com.sun.script.javascript.ExternalScriptable
    at com.sun.script.javascript.RhinoScriptEngine.<init>(RhinoScriptEngine.java:43)
    at com.sun.script.javascript.RhinoScriptEngineFactory.getScriptEngine(RhinoScriptEngineFactory.java:51)
    at javax.script.ScriptEngineManager.getEngineByName(ScriptEngineManager.java:154)
    at test.app.Test.execute(Test.java:33)
    at test.app.Test.scriptMethod(Test.java:23)
    at test.app.Test.onCreate(Test.java:17)
    [...]

When using JavaScript as ScriptEngine name:

09-20 11:16:16.883 8776-8776/? I/TEST: using Script engine name: JavaScript
09-20 11:16:16.883 8776-8776/test.app E/AndroidRuntime: FATAL EXCEPTION: main
  Process: test.app, PID: 8776
  java.lang.RuntimeException: Unable to start activity ComponentInfo{test.app/test.app.Test}: java.lang.UnsupportedOperationException: Engine not found: JavaScript
      [...]
   Caused by: java.lang.UnsupportedOperationException: Engine not found: JavaScript
      at test.app.Test.execute(Test.java:37)
      at test.app.Test.scriptMethod(Test.java:23)
      at test.app.Test.onCreate(Test.java:17)
      [...]

As you can see rhino is trying to instanciate a ScriptEngine but a class throw an error when loaded (com.sun.script.javascript.ExternalScriptable).

The thing is that your jar dependency contains the JSR223 and some engine definition but does not contain the actual script engine.

To correct this situation you may want to add an actual rhino implementation to your dependencies.

aveuiller
  • 1,511
  • 1
  • 10
  • 25
  • I use jsr-223-1.0-pr.jar from http://www.java2s.com/Code/Jar/j/Downloadjsr22310prjar.htm – pmf Sep 20 '17 at 13:25
  • By decompiling the `RhinoScriptEngineFactory` in the jar file you provided, we can see that the engine names are `js` and `rhino`. Can you try with one of these? – aveuiller Sep 20 '17 at 13:41
  • with both of them is null too. the error is" at com.sun.script.javascript.RhinoScriptEngine.(RhinoScriptEngine.java:43)" and "at com.sun.script.javascript.RhinoScriptEngineFactory.getScriptEngine(RhinoScriptEngineFactory.java:51)" – pmf Sep 20 '17 at 13:55
  • I thought your `Object result`was null, but you are having an exception, aren't you? If so can you put the complete stacktrace in your question? – aveuiller Sep 20 '17 at 14:18
  • the exceptions are when i use js and rhino . – pmf Sep 20 '17 at 14:38
  • I tried your solution and several solutions, but did not get any results...:( – pmf Sep 20 '17 at 19:56
  • It's weird because I made it work with the library I linked to you in thre first place, see [this gist](https://gist.github.com/aveuiller/9cc1a56ea8f75db8732521d8c250e27a) for the relevant code sample. – aveuiller Sep 21 '17 at 08:52
  • Thank you very much Antoine. Your answers helped me a lot. I just found a new solution to my problem here https://stackoverflow.com/a/42563063 Thanks again . – pmf Sep 21 '17 at 08:58