evaluate javascript file - Java javax.script

Java examples for javax.script:ScriptEngine

Description

evaluate javascript file

Demo Code


//package com.java2s;
import java.io.FileReader;

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

public class Main {
    public static Object getValue(String filepath, String key) {
        try {// www. j  a  va  2 s . c o m
            ScriptEngineManager sem = new ScriptEngineManager();
            ScriptEngine jsEngine = sem.getEngineByName("js");
            jsEngine.eval(new FileReader(filepath));
            return jsEngine.get(key);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials