日期:2014-05-16  浏览次数:20337 次

Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型
package com.sino.java7;

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

/**
 * Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型
 * @author ylchou@qq.com
 *
 */
public class JavaScriptEngine{

	public static void greet() throws ScriptException{
		ScriptEngineManager manager = new ScriptEngineManager();
//		ScriptEngine engine = manager.getEngineByExtension("js");
//		ScriptEngine engine = manager.getEngineByMimeType("text/javascript");
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		
		if(engine == null){
			throw new RuntimeException("not found JavaScript engine!");
		}
		
		engine.eval("println('hello,java7!')");
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws ScriptException{
		greet();
	}

}


 

/**
 hello,java7!
 */