Scripting language based on Java 6.0 Operation



Java6.0 which added a very useful package: javax.script, it is the action script of the new Java package, we can use it to operate on the scripting language, such as modifying, or call, and the Java language and can interactIf we use good, we use it to achieve some of the most frequently changed, so that we can write some algorithms inside the js file and then running out of time to read and execute, so that eliminates the need for changeSome things need to recompile the process of the.

Here we look at an example, and see how the Java code inside the action script and calls inside the method.

/ *

* Test.java

*

* Created on 2007-9-19, 15:28:49

*

* To change this template, choose Tools | Templates

* And open the template in the editor.

* /

package lbf.script;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

import javax.script.Bindings;

import javax.script.Invocable;

import javax.script.ScriptContext;

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.swing.JFrame;

/ **

*

* @ Author hadeslee

* /

public class Test {

public static void main (String [] args) throws Exception {

/ / Js suffix under the name of the script to generate a parsing engine parse JS

ScriptEngine engin = new ScriptEngineManager (). GetEngineByExtension ("js");

/ / Query about this engine is very useful to achieve the following interface

System.out.println (engin instanceof Invocable);

/ / Declare two objects, passed to JS in there

JFrame jf = new JFrame ("test");

List list = new ArrayList ();

/ / Set by Peng key object, the object into the current two to JAVA

Bindings bind = engin.createBindings ();

bind.put ("jf", jf);

bind.put ("list", list);

/ / Key under the Bang object into them, the scope is the scope of the current engine

engin.setBindings (bind, ScriptContext.ENGINE_SCOPE);

/ / Use the engine to execute a document written in JS code inside the

Object obj = engin.eval (new FileReader ("test.js"));

/ / This time, of course, is null the return value

System.out.println (obj);

/ / The current forced into the engine Invocable, so that you can define in the JS file call a function inside a

Invocable in = (Invocable) engin;

/ / Get the inside from the JS object returned,

List l = (List) in.invokeFunction ("getNames");

System.out.println (l);

/ / Call about the definition inside another function in JS

in.invokeFunction ("testJS");

/ / Finally, call a function that allows us to define the form displayed in front of

in.invokeFunction ("doSth");

}

}

Here is the definition of the content inside the test.js

function doSth () {

jf.setSize (500,300);

jf.setVisible (true);

jf.setDefaultCloseOperation (jf.EXIT_ON_CLOSE);

}

function getNames () {

list.add ("doSth");

list.add ("getNames");

return list;

}

function testJS () {

print ('Hello world!');

}

We can see that after running in JAVA, the form will be displayed, and we can receive from the JS parsing engine which returns the data, of course, we can also call a very common JS functions, imagine, if we put ourprogram is running some of the objects are located to the Bindings to go inside, then we are not that great JS freedom yet? because JS can operate inside our Java objects, and we can program the same as the ava on JS programming, and also no need to compile, we can immediately run. flexibility it not become higher yet?

In a few days to write a JS analysis using Java programming flexibility to improve the example to reflect about the usefulness of this package, however, JS drawback is that the implementation was slow, slower than the Java code that is more, but some initialthings, or some things set, we do not have coded the program inside, can we define the JS file from which to read, after all, means that only once in exchange for high efficiency at the expense of flexibility isworthwhile.