使用Java调用JS


import junit.framework.TestCase;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

public class CallJs extends TestCase {
public void testConsoleLog() throws ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    //此处会抛出异常,Java的script引擎并不包含console对象
    engine.eval("console.log('hello world')");
}

public void testConsoleLog2() throws ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    engine.put("console", System.out);
    //此处会抛出异常,Java的script引擎并不包含console对象
    engine.eval("console.println('hello world')");
}

public void testUseJsFunction() throws ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    engine.eval("function add(x,y){return x+y}");
    Object value = engine.eval("add(3,7)");
    System.out.println(value.getClass() + " " + value);
}

/**
 * 注意,这个内部类必须是public否则js编译器看不见
 */
public class SubWrapper {
    public int sub(int x, int y) {
        return x - y;
    }
}

public void testUseJsFile() throws IOException, ScriptException {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    InputStream cin = getClass().getResourceAsStream("/calljs.js");
    InputStreamReader reader = new InputStreamReader(cin);
    engine.put("subWrapper", new SubWrapper());
    engine.eval(reader);
    engine.put("console", System.out);
    System.out.println(engine.eval("add(3,4)"));
    System.out.println(engine.eval("sub(5,6)"));
}

public void testUpdateJavaObject() throws ScriptException {
    JButton button = new JButton();
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
    engine.put("button", button);
    engine.eval("button.text='hello world'");
    System.out.println(button.getText());
}

public void testlistAllScriptEngine() {
    //列出全部脚本引擎
    new ScriptEngineManager().getEngineFactories().forEach(fac -> {
        System.out.println("name " + fac.getEngineName()
                + "\nlanguage " + fac.getLanguageName()
                + "\nversion " + fac.getEngineVersion()
                + "\nlanguageVersion " + fac.getLanguageVersion()
                + "\nextensions " + fac.getExtensions().stream().collect(Collectors.joining(","))
                + "\nmimetypes " + fac.getMimeTypes().stream().collect(Collectors.joining(","))
                + "\n"
        );
    });
}

}

js

function add(x, y) {
    return x + y
}

function sub(x,y){
    return subWrapper.sub(x,y)
}

转载于:https://www.cnblogs.com/weiyinfu/p/10021395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值