4
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

Pavya
  • 5,895
  • 4
  • 26
  • 40
rrkjonnapalli
  • 109
  • 1
  • 6

5 Answers5

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);
Halil RAŞO
  • 207
  • 4
  • 12
4

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
Miko Chu
  • 319
  • 3
  • 14
1

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.

rsommerard
  • 382
  • 1
  • 4
  • 14
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");
Rahul
  • 307
  • 3
  • 11
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
Jorgesys
  • 114,263
  • 22
  • 306
  • 247