Created
January 11, 2020 02:37
-
-
Save raicerk/2a3033e969849bb14f973c3a21be57ca to your computer and use it in GitHub Desktop.
Evaluar expresiones de texto como funciones en java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.script.Invocable; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
class Main { | |
public static void main(String[] args) { | |
try { | |
ScriptEngineManager mgr = new ScriptEngineManager(); | |
ScriptEngine engine = mgr.getEngineByName("JavaScript"); | |
engine.put("_nombre_","juan"); | |
engine.put("_apellido_", "mora"); | |
Object res = engine.eval("_apellido_ != '' ? 'wiiii' : 'nooooo';"); | |
System.out.println(res.toString()); | |
}catch(Exception error){ | |
System.out.println("hubo un error"); | |
System.out.println(error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment