Java 5.0 in the realization of JSR-223 Method
Abstract: In many cases, the use of the Java scripting language will be very useful, for example, a Java application program expansion, so users can write their own scripts for expansion or custom core functions.
Such as PHP, Ruby, JavaScript, Python (or Jython) like scripting language is widely used in many fields, and because of its flexibility and simplicity and popular. The script is explained rather than being compiled, so it's easy to run from the command line and testing them. This reduced the code / test cycle, and improve the productivity of the development staff. Script type usually is a dynamic, highly expressive syntax, prepared by the algorithm than the equivalent in Java much more simple algorithm. Using them usually very interesting.
In many cases, the use of the Java scripting language will be very useful, for example, a Java application program expansion, so users can write their own scripts for expansion or custom core functions. Scripting language and greater readability and easier to prepare, so (technically) they are used to provide end-user demand for customized products under the possibility of an ideal language.
Java already available there are many independent scripting package, including Rhino, Jacl, Jython, BeanShell, such as JRuby. Java 6 is the news through a standard interface for scripting language provides a built-in support.
Java 6 provides the JSR-223 standard full support. The standard provides an implementation of the Java scripting language within the convenience of standard, and to provide access from within Java script of resources and functions. Java 6 with the Mozilla Rhino fringe of the built-in JavaScript to achieve integration. Based on the specification, such as PHP, and BeanShell Groovy like other scripting language support also in progress. This paper is concerned about the realization of Rhino, but other languages should be essentially the same.
How to use Java 5 in the JSR-223 Implementation? Even after careful study, they finally found the solution ….
1. Theoretical work from the beginning ….
According to the statement JavaDoc, javax.script.ScriptEngineManager JAR norms through the so-called service provider (Service Provider) method at the library retrieval javax.script.ScriptEngineFactory achieve category and automatically registered. OK, understand this can be easily handled the , ready to start work Kazakhstan ….
2. Started work the hands ….:)
First production JSR-223 standard achievable package (including SUN achieve the JavaScript default):
WinRAR compression tools such as the use of open document JRE6 in rt.jar (Note to advance backup! I is the backup for jsr-233.jar), as shown in Figure 1:
Figure 1 rt.jar the directory structure
OK, delete files …. Below began addition com.sun.script, sun.org, javax.script, META-INF directory, the directory of all the remaining deleted. Deletion as shown below:
Figure 2 delete the directory structure
Contents do a good job, in accordance with the following JAR began regulating Service Provider described methods to create documents to facilitate the retrieval javax.script.ScriptEngineManager:
META-INF in the establishment of a directory of services for the directory name.
In the name of the new directory services for javax.script.ScriptEngineFactory empty document.
In javax.script.ScriptEngineFactory paper, write "com.sun.script.javascript.RhinoScriptEngineFactory" data, to not written quotes.
Figure 3 the addition of new services directories and files
Finally, all changes will be kept to jsr-223.jar document, the work is completed!
3. Test kits
Jsr-223.jar will be added to the library project, and then the preparation of the code below:
<table BorderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td> ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName ( "js");
Engine.put ( "age", 21);
Engine.eval ( "if (age> = 18) (" +
"Print ( 'Old enough to vote!');" +
") Else (" +
"Print ( 'Back to school !');" +
"}");</ Td> </ tr> </ table>
Then console should be returned to: Old enough to vote! Below are run in the Eclipse of the title:
Figure 4 testing procedures in the operation of Eclipse ↑ Back
Tags: java method, the method
Releated Java Articles
June 16, 2007 | Filed Under Core Java |






