You can use ScriptEngine, example:
public class Main {
public static void main(String[] args) {
StringBuffer javascript = null;
ScriptEngine runtime = null;
try {
runtime = new ScriptEngineManager().getEngineByName("javascript");
javascript = new StringBuffer();
javascript.append("1 + 1");
double result = (Double) runtime.eval(javascript.toString());
System.out.println("Result: " + result);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}