JDK 6 - 04: Scripting : InvokeScriptFunction

0

Written on 오전 2:38 by 강여사(J.Y.Kang)

스크립트 함수와 메소드 호출하기

때때로 특정 스크립트 함수를 반복하여 호출하고자 할 때가 있다 – 예를 들어, 스크립트로 구현된 어플리케이션 메뉴 기능 등등. 여러분의 메뉴 액션 이벤트 핸들러 안에, 특정 스크립트 함수를 호출하길 원할 경우, 다음 예제의 자바코드로부터 특정 스크립트 함수를 호출하는 방법으로 사용하면 된다.

===============================================================================
package test;

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

public class InvokeScriptFunction {

public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");

// JavaScript code in a String
String script = "function hello(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);

// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
Invocable inv = (Invocable) engine;

// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!");
}
}
===============================================================================

If you enjoyed this post Subscribe to our feed


No Comment

댓글 쓰기