How to use ScriptEngineManager in Android?
Asked Answered
D

5

5
import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

 public void bEqual(View v) throws ScriptException {

       ScriptEngineManager mgr = new ScriptEngineManager();
       ScriptEngine engine = mgr.getEngineByName("JavaScript");
        String value = inputText.getText().toString();
        Toast.makeText(this,value,Toast.LENGTH_LONG).show();
        try{
            result = (double)engine.eval(value);
            Toast.makeText(this,String.format("%f",result),Toast.LENGTH_SHORT).show();
        }catch(Exception e){
            Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
        }

    }

What is wrong in it? App is exiting when perform this action. It is not showing any errors but app is closing

Distend answered 9/2, 2017 at 5:6 Comment(0)
P
7

Easiest way is to use Mozilla Rhino in Android instead[1] because ScriptEngine and it's dependencies would require time consuming setup and headache.

Import the ff:

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import android.util.Log;

Code you put somewhere in your method:

    Context context = Context.enter(); // 
    context.setOptimizationLevel(-1); // this is required[2]
    Scriptable scope = context.initStandardObjects();
    Object result = context.evaluateString(scope, "18 > 17 and 18 < 100", "<cmd>", 1, null);
    Log.d("your-tag-here", "" + result);

Add it in your gradle dependencies.

implementation group: 'org.mozilla', name: 'rhino', version: '1.7.10'

Reference:

  1. Mozilla Rhino
  2. Explanation why that line of code is needed
  3. Mozilla Rhino's Maven Repository
Pardoner answered 30/10, 2018 at 7:1 Comment(2)
Might also add --> import android.util.Log; among imports.Siler
Ok, added the import.Pardoner
S
4

first add this: compile 'io.apisense:rhino-android:1.0' to your app gradle file. Then type this snippet code

    TextView resultTextView = findViewById(R.id.result_text_view);
    String currentText = resultTextView.getText().toString();
    boolean valid = checkForValidity();
    double result=0;
    if(valid){
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
        try{
            result = (double)engine.eval(currentText);
        }catch(Exception e){
            Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
        }
        currentText = currentText +"\n"+ "="+(result);
    }
    resultTextView.setText(currentText);
Slone answered 8/1, 2018 at 8:45 Comment(0)
L
2

The javax.script package is not available on Android.

If you want to use a Javascript engine, you must implement those behaviours by yourself or use one of the dependencies that already exist like rhino-android.

Lylalyle answered 20/4, 2017 at 15:2 Comment(0)
M
1

javax.script not available in android that's why you have to use third party library

I am using "rhino-android" and it serve the purpose

https://github.com/APISENSE/rhino-android

implementation in gradle file

implementation 'io.apisense:rhino-android:1.0'

java code

javax.script.ScriptEngine engine = new javax.script.ScriptException().getEngineByName("rhino");
Object result = engine.eval("2+2");
Maloney answered 25/5, 2018 at 6:40 Comment(0)
M
1

This is a real example using Rhino Script Engine in Android in Android

Use this imports:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

and this is an example:

           String Operation = "5+4-2";
           ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");

            try {
               Object result = engine.eval(Operation);
                Log.d("Calculator", "Operation: " + Operation + " result: " + result);
            } catch (ScriptException e) {
                Log.d("Calculator", " ScriptEngine error: " + e.getMessage());
            }

Output:

Operation: 5+4-2 result: 7.0
Munson answered 31/10, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.