zoukankan      html  css  js  c++  java
  • JDK1.7中调用javascript方法

    import java.io.File;
    
    import javax.script.Invocable;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    
    public class TestJs {
        public static void main(String[] args) throws Exception {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            testScriptVariables(engine);// 演示如何暴露Java对象为脚本语言的全局变量
            testInvokeScriptMethod(engine);// 演示如何在Java中调用脚本语言的方法
            testScriptInterface(engine);// 演示脚本语言如何实现Java的接口
            testUsingJDKClasses(engine);// 演示脚本语言如何使用JDK平台下的类
        }
    
        public static void testScriptVariables(ScriptEngine engine)
                throws ScriptException {
            File file = new File("test.txt");
            engine.put("f", file);
            engine.eval("println('Total Space:'+f.getTotalSpace())");
        }
    
        public static void testInvokeScriptMethod(ScriptEngine engine)
                throws Exception {
            String script = "function hello(name) { return 'Hello,' + name;}";
            engine.eval(script);
            Invocable inv = (Invocable) engine;
            String res = (String) inv.invokeFunction("hello", "Scripting");
            System.out.println("res:" + res);
        }
    
        public static void testScriptInterface(ScriptEngine engine)
                throws ScriptException {
            String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
            engine.eval(script);
            Object obj = engine.get("obj");
            Invocable inv = (Invocable) engine;
            Runnable r = inv.getInterface(obj, Runnable.class);
            Thread th = new Thread(r);
            th.start();
        }
    
        public static void testUsingJDKClasses(ScriptEngine engine)
                throws Exception {
            // Packages是脚本语言里的一个全局变量,专用于访问JDK的package
            String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
            engine.eval(js);
            Invocable inv = (Invocable) engine;
            inv.invokeFunction("doSwing", "Scripting Swing");
        }
    }
  • 相关阅读:
    ubuntu12.04 死机 卡屏 画面冻结解决方案
    Install Firefox 20 in Ubuntu 13.04, Ubuntu 12.10, Ubuntu 12.04, Linux Mint 14 and Linux Mint 13 by PPA
    ListView1.SelectedItems.Clear()
    android studio 下载地址
    jquery.slider.js jquery幻灯片测试
    jquery.hovermenu.js
    jquery.tab.js选项卡效果
    适配 placeholder,jquery版
    jquery.autoscroll.js jquery自动滚动效果
    将 Google Earth 地图集成到自己的窗体上的 简单控件
  • 原文地址:https://www.cnblogs.com/sm21312/p/4066426.html
Copyright © 2011-2022 走看看