java scriptEngine支持groovy,python,jexl,JavaScript 的交互

package com.moodincode.database_git_compare;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ScriptEngineTest {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        /*
               <dependency>
                    <groupId>org.python</groupId>
                    <artifactId>jython</artifactId>
                    <version>2.7-b1</version>
                </dependency>
        */
        //PyScriptEngine
        ScriptEngine engine = manager.getEngineByName("python");
        try {
            //模拟脚本中调用传入的变量
            engine.put("test", new ScriptEngineTest());
            engine.eval("print \"hello \"+test.test(\"python\")");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
       /*
                     <dependency>
                            <groupId>org.codehaus.groovy</groupId>
                            <artifactId>groovy-all</artifactId>
                            <version>3.0.5</version>
                            <type>pom</type>
                        </dependency>
        */
        //GroovyScriptEngineImpl
        ScriptEngine engine2 = manager.getEngineByName("groovy");
        String HelloLanguage = "def hello(language) {return \"Hello $language\"}";
        engine2.put("test", new ScriptEngineTest());
        Object eval = engine2.eval(HelloLanguage);
        eval = engine2.eval("hello(\"Groovy\")");
        System.out.println(eval);
        eval = engine2.eval("test.test2(\"33333#adaadssssssssss#\")");
        System.out.println(eval);
        /*
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-jexl3</artifactId>
          <version>3.1</version>
       </dependency>
       */
        //JexlScriptEngine
        ScriptEngine engine3 = manager.getEngineByName("jexl");
        try {
            engine3.put("test", new ScriptEngineTest());
            engine3.eval("test.test(\"jexl\")");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

         /* jdk15以下不需要引入这个依赖,jdk拓展包自带
         <dependency>
          <groupId>org.openjdk.nashorn</groupId>
          <artifactId>nashorn-core</artifactId>
          <version>15.4</version>
       </dependency>
        */
        //NashornScriptEngine
        ScriptEngine engine4 = manager.getEngineByName("javascript");
        //或者ScriptEngine engine4 = manager.getEngineByName("nashorn");
        try {
            engine4.put("test", new ScriptEngineTest2());
            Object evalValue = engine4.eval("var j='hello js '+test.test();j;");
            System.out.println(evalValue);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //输出结果为:test
        //test
        //hello hello worl====python
        //Hello Groovy
        //test
        //hello worl====33333#adaadssssssssss#
        //Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
        //test
        //test
        //hello js hello worldasssasaaa
        //Process finished with exit code 0


    }

    public String test() {
        System.out.println("test");
        return "hello worldasssasaaa";
    }

    /**
     * 暴露变量给脚本调用
     *
     * @param key
     * @return
     */
    public String test(String key) {
        System.out.println("test"
        );
        return "hello worl====" + key;
    }

    public String test2(String key) {
        System.out.println("test"
        );
        return "hello worl====" + key;
    }
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心情加密语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值