invoke javascript Function - Java javax.script

Java examples for javax.script:ScriptEngine

Description

invoke javascript Function

Demo Code


//package com.java2s;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Main {
    public static Object invokeFunction(String filepath, String funcname,
            Object... params) {/*  w w  w.  jav a  2 s. c  o m*/
        try {
            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine jsEngine = sem.getEngineByName("js");
            jsEngine.eval(new FileReader(filepath));
            Invocable invocable = (Invocable) jsEngine;
            return invocable.invokeFunction(funcname, params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials