To add a scripting engine to a Java application

Source: Internet
Author: User

Objective

Modern many popular applications, more and more use of the scripting engine, the most typical of Microsoft office, such as VBA. The scripting engine provides great scalability for applications and is a pleasure to be seen by many users who are hot and loyal to two of developers. This article focuses on BeanShell----Such a Java application scripting engine, you will understand its basic features, and how to embed it in your application. You'll see how easy it is to add a scripting engine to your application.

Common scripting engine

Now there are many scripting languages on the web, such as Tcl,perl, Javascript,python, and many scripting languages have java-based interpreters, as far as the author knows:

Language Java implementation
Javascript Rhino
Python Jython (formerly Jpython)
Tcl/tk Jacl
Perl None
Java itself BeanShell

Each of these scripts has its own syntax, and JavaScript and BeanShell syntax are more intimate for the user. This article focuses on the characteristics of BeanShell and how to integrate it into a Java application.

What is BeanShell?

BeanShell is a small, free, embeddable Java code interpreter with object-oriented scripting language features. It is written in the Java language. It performs standard Java statements and expressions, with simple script commands and syntax. It treats programming objects as a simple method, much like Perl and JavaScript.

You can use BeanShell when writing Java tests or debugging, or you can use it as a script for your application. In short, BeanShell can dynamically interpret the Java language. That is to say, BeanShell is useful to Java in many ways like TCL/TK for C: BeanShell is embeddable---you can invoke BeanShell from your application at runtime to execute dynamically Java code or provide a script extension for your application. Instead, you can call your application and its objects from BeanShell, which allows Java objects and APIs to run dynamically. Because BeanShell is written in Java, it can run in the same JVM space as your application, and you can also freely pass the reference (References) of the real-time object to the script code and return as the result.

Introduction to BeanShell scripting languages

BeanShell can understand standard Java statements, expressions, and method declarations. The contents of statements and expressions can be: variables, declarations, assignments, method calls, loops, conditions, and so on.

You have to use them strictly in Java programs, but in BeanShell you can use them in the form of "loose type" (loosely typed). That is, you can be lazy in writing scripts without declaring variable types (both in original data types and objects). If you try to use the wrong variable type, BeanShell will give you an error. In short BeanShell scripts are easy to get started with.

Here's a simple example:

foo = "Foo";
four = (2 + 2)*2/2;
print( foo + " = " + four ); // print() 是BeanShell的一个 脚本命令。
// 循环
for (i=0; i<5; i++)
   print(i);
//显示一个有包含一个按钮的窗口
button = new JButton( "My Button" );
frame = new JFrame( "My Frame" );
frame.getContentPane().add( button, "Center" );
frame.pack();
frame.setVisible(true);

You can also declare and define your own method in the BeanShell script:

int multiply(int a, int b)
{
   return a*b;
}
print(multiply(12,14));//打印出168

The type of the BeanShell variable or parameter can be specified without display, as follows:

int multiply(a, b)
{
   return a*b;
}
result = multiply(12,14);
print(result);//打印出168

Related Article

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.