I have been trying the below code
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptCode {
public static void main(String[] args) throws Exception{
String code="";
int y=-1;
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
code="if(a<b){return b;}else{return a;}";
engine.put("a",10);
engine.put("b",100);
y=(int)engine.eval(code);
System.out.println(y);
}
}
I am getting an error saying
Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:1:8 Invalid return statement
if(a<b){return b;}else{return a;}
I am not able to resolve this issue. The issue is that in my usecase, the 'code' variable will have some rules, which will return something. I know how to do it without returning, but I am unable to return some value from the code. How can I do it?