Introduction to the java operation script package script

Source: Internet
Author: User

Java6.0 adds a very practical package: javax. script, which is a new Java operation script package. With this package, we can perform operations on the script language, such as modifications or calls, and interact with the Java language, if we make good use of it, we can use it to implement some frequently-modified parts, so that we can write some algorithms into the js file, then read and execute it during running, saving the need to re-compile the changes.

Let's look at an example to see how to operate scripts in Java code and call the methods in them.

/*
* Test. java
*
* Created on, 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 {
// Generate a parsing js script parsing engine based on the JS suffix
ScriptEngine engin = new ScriptEngineManager (). getEngineByExtension ("js ");
// Check whether the engine has implemented the following useful interfaces.
System. out. PRintln (engin instanceof Invocable );
// Declare two objects and pass them into JS
JFrame jf = new JFrame ("test ");
List List = new ArrayList ();
// Obtain the bounded key-value object and put the current two JAVA objects
Bindings bind = engin. createBindings ();
Bind. put ("jf", jf );
Bind. put ("list", list );
// Put the key-value object under the condition, and the scope is the scope of the current engine.
Engin. setBindings (bind, ScriptContext. ENGINE_SCOPE );
// Execute a piece of code written in the JS file using the engine
Object obj = engin. eval (new FileReader ("test. js "));
// The returned value is null at this time.
System. out. println (obj );
// Convert the current engine to Invocable, so that you can call a function defined in the JS file.
Invocable in = (Invocable) engin;
// Get the object returned from JS
List L = (List ) In. invokeFunction ("getNames ");
System. out. println (l );
// Call another function defined in JS
In. invokeFunction ("testJS ");
// Call a function to display the previously defined form.
In. invokeFunction ("dosomething ");
}
}

The following content is defined in test. js.

Function dosomething (){
Jf. setSize (500,300 );
Jf. setVisible (true );
Jf. setdefaclocloseoperation (jf. EXIT_ON_CLOSE );
}
Function getNames (){
List. add ("dosomething ");
List. add ("getNames ");
Return list;
}

Function testJS (){
Print ('Hello world! ');
}

We can see that after JAVA runs, the form will be displayed, and we can receive the data returned from the JS parsing engine, of course, we can also call a very common JS function. Imagine that if we set some of the objects when we run the program to Bindings, so isn't JavaScript having a great degree of freedom? Because JS can also operate on our Java objects, and we can program JavaScript like ava, and run it immediately without compilation. isn't flexibility ever higher?

In a few days, I will write an example that uses Java to parse JS to improve programming flexibility to demonstrate the usefulness of this package. However, the disadvantage of JS is that the execution speed is slow, which is much slower than that of Java code, but some initialization tasks or some setup tasks do not need to be written into the program, so we can read them from the JS file we defined, it is worthwhile to sacrifice a little efficiency for those methods that only run once in exchange for high flexibility.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.