Java from the box does not know how, because it is in Java, IMHO, is not necessary. But you can run JavaScript java, but he already knows it :
import javax.script.ScriptEngineManager; import javax.script.ScriptEngine; public class Test { public static void main(String[] args) throws Exception{ ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String foo = "40+2"; System.out.println(engine.eval(foo)); } }
Or try libu js-evaluator-for-android
We connect
build.gradle project file:
allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } }
build.gradle app module file:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // Keep you existing dependencies here compile 'com.github.evgenyneu:js-evaluator-for-android:v1.0.7' }
We use:
JsEvaluator jsEvaluator = new JsEvaluator(this); jsEvaluator.evaluate("2 * 17", new JsCallback() { @Override public void onResult(final String result) { // Process result here. // This method is called in the UI thread. Log.d("MyTag", result); } });