JAVA动态 运算符判断要借用 js
当我写下下面代码
ScriptEngineManager manager = new ScriptEngineManager(); // 创建脚本执行引擎 ScriptEngine engine = manager.getEngineByName("js"); // 执行逻辑判断 boolean ret = (boolean) engine.eval("1 == 2"); boolean ret2 = (boolean) engine.eval("2 == 2 && 1 == 1"); boolean ret3 = (boolean) engine.eval("2 == 2 || 1 == 2 && ( 3==3 )");
居然报engine 是null值
百度了十分钟,终于找到
JavaScript引擎在高版本移除掉了,所以报这个错误
于是添加引用
<dependency> <groupId>org.openjdk.nashorn</groupId> <artifactId>nashorn-core</artifactId> <version>15.4</version> </dependency>
代码即可运行
标签:engine,JAVA17,&&,nashorn,JS,boolean,eval,ScriptEngine From: https://www.cnblogs.com/cvol/p/17009537.html