Scripting Scripting within Java

황제낙엽 2012.01.03 14:38 조회 수 : 393

sitelink1 https://wikis.oracle.com/display/code/Scripting+within+Java 
sitelink2  
sitelink3 http://1 
sitelink4 http://ko 
sitelink5  
sitelink6 http://sitelink1 

The following code sample is from How Java Plays With Scripting Languages

 

The Java Scripting Framework provides a set of scripting APIs, which allow Java Scripting Engines to be used in Java applications. The API is intended for use by application programmers who wish to execute programs written in scripting languages, in their Java applications. Let's go through a code sample to see how this can be achieved.

 

The simplest way to use the scripting API is as follows:

 

1. Create a ScriptEngineManager object.
2. Get a ScriptEngine object from the manager.
3. Evaluate the script using the ScriptEngine's eval methods.

import javax.script.*;
public class EvalScript {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");

        // create a Java object
        String name = "Tom";

        // create the binding
        engine.put("greetingname", name);

        // evaluate JavaScript code from String
        engine.eval("println('Hello, ' + greetingname)");
        engine.eval("println('The name length is ' +  greetingname.length)");
    }
}

 

The output is:

Hello, Tom
The name length is 3

 

Let's go through the code sample:

 

1. The Script Engine Discovery Mechanism identifies the Script Engine by the script name "JavaScript".
2. Create the bindings with key/value pairs, the key is "greetingname" JavaScript variable name, the value is the Java object proxy, which wraps the Java String "name".
3. The "eval()" method of Script Engine parses the JavaScript code and executes it by internally using a front-end compiler and back-end executor.
4. During the execution, the method and attribute of the Java object can be accessed from the script.

번호 제목 글쓴이 날짜 조회 수
223 String to InputSource 황제낙엽 2012.12.03 601
222 Class.getResource() vs. ClassLoader.getResource()/getResources() 황제낙엽 2012.06.24 466
221 Jar파일에 포함된 리소스 접근하는 방법(How to read a resource from a JAR file ) file 황제낙엽 2012.06.24 475
220 Java에서 URL 다루기 file 황제낙엽 2012.06.24 396
219 HttpServletResponse.setContentType(java.lang.String type) 과 MIME 타입 황제낙엽 2012.04.20 509
218 code, codebase 속성과 applet object 동적 생성 file 황제낙엽 2012.04.17 451
217 한글 파일명 깨짐으로 살펴본 다국어 처리 문제 (UTF-8) 황제낙엽 2012.03.22 10403
216 BufferedReader.readLine() hangs 황제낙엽 2012.02.23 809
215 OS 쉘 명령어(shell script) 실행하기 [ProcessBuilder, Runtime.getRuntime().exec()] 황제낙엽 2012.02.22 923
214 PreProcess 실행 (전처리기 만들기) file 황제낙엽 2012.01.04 478
213 javax.script와 타입변환 황제낙엽 2012.01.03 430
» Scripting within Java 황제낙엽 2012.01.03 393
211 Hex string <-> String object 황제낙엽 2011.11.29 611
210 An alternative to the deep copy technique file 황제낙엽 2011.07.27 570
209 <jsp:include>, include지시자 file 황제낙엽 2011.07.24 438
208 <jsp:include> 액션태그 황제낙엽 2011.07.24 403
207 volatile 에 대한 단상(斷想) 황제낙엽 2011.06.22 426
206 Object element 의 onerror 이벤트 황제낙엽 2011.04.21 295
205 Java 2D Graphics - Reference link 황제낙엽 2011.04.11 459
204 deployJava.js를 이용한 JRE 자동설치 및 Applet 디플로이 file 황제낙엽 2011.04.04 943