Four Java scripting languages

Source: Internet
Author: User
Tags java jframe

Source cactus studio enet power in Silicon Valley

I. script interpreter Overview

In some java application requirements, the integration of a script language can bring great convenience. For example, you may want to write a script program to drive the application, expand the application, or write loop and other process control logic to simplify the operation. In these cases, an ideal solution is to provide support for the script interpreter in Java applications, let the script language interpreter read the scripts written by the user and run these scripts on the classes provided by the application. To achieve this goal, you can run a Java-based script language interpreter in the JVM where the Java application runs.

Some supported libraries, such as the IBM bean Scripting framework, can help you integrate different scripting languages into Java programs. These support frameworks allow your Java applications to run scripts written in TCL, Python, and other languages without making many changes.

After a script interpreter is integrated into a Java application, user-written scripts can directly reference classes of Java applications, just as these scripts are part of Java programs. This approach has both advantages and disadvantages. The advantage is that if you want to perform a regression test on the application in a script-driven manner or use a script to perform a low-level call to the application, it can bring great convenience. The disadvantage is that, if your script directly operates the internal structure of the Java program rather than the approved API, it may affect Java program integrity and application security. Therefore, you should carefully plan the APIs that allow users to write scripts for them and declare that the rest of the program cannot be operated by scripts. In addition, you can also perform fuzzy processing on the names of classes and methods that do not require users to perform Script Programming on them, leaving only the names of API classes and methods that allow script programming. In this way, you can effectively reduce the possibility of risky users directly using scripts to operate protected classes and methods.

It is of extraordinary significance to support multiple scripting languages in Java programs, but if you are writing a commercial application, consider it with caution-although you provide users with the most complete functions, but it also brings the most error opportunities. Configuration and management issues must be taken into account, because at least some script interpreters are regularly upgraded and updated, in this way, you must make great efforts to manage which versions of each interpreter are suitable for Java applications. If you upgrade a script interpreter to solve the bug in the old script interpreter, your Java application will run in an untested configuration. After several days or weeks, users may find problems due to the upgrade of the script engine, but they may not tell you about the upgrade of the script engine, at this time, it is difficult for you to repeat the error reported by the user.

In addition, users may insist that you must provide patches for the script interpreter supported by Java applications. Some script interpreters are maintained and updated in a timely manner in the Open Source mode. For these script interpreters, experts may help you solve the problem and fix the interpreter, or introduce patches in a new release. This is very important, because the script interpreter is a very complicated tool that contains a lot of code. Without expert support, it is a troublesome task to modify the script interpreter by yourself, you may be helpless.

To avoid this problem, you should perform a comprehensive test on each script interpreter that is prepared to provide support in Java applications. For each interpreter, ensure that it can smoothly handle the vast majority of common use cases, and ensure that it runs a large number of scripts even under extremely harsh conditions, there will be no large memory vulnerabilities, make sure that there is no unexpected situation when you perform a rigorous beta test on the Java program and script interpreter. Of course, this preliminary test requires time and other resources; however, the test investment is always worthwhile.
Ii. Keep the system simple

If you must provide script support in a Java application, you must first select a script interpreter that best meets the application requirements and the user base. Choosing the right interpreter can simplify the code of the integrated interpreter, reduce the expenditure on customer support, and improve the stability of the application. The most difficult question is: which interpreter should be used if only one interpreter can be used?

I have compared several script interpreters. At the beginning, I considered the scripting languages including TCL, Python, Perl, JavaScript, and beanshell. Then, before in-depth analysis, I gave up Perl. Why? Because Perl does not use an interpreter written in Java. Assume that you have selected a script interpreter implemented using the local code, such as Perl, then the interaction between the Java application and the script code will not proceed directly. In addition, every operating system you want to support must provide a binary code library of the script interpreter. Many developers choose Java because of its cross-platform portability. To ensure that Java applications have such advantages, it is best to choose an interpreter that does not depend on the local code. Unlike Perl, TCL, Python, JavaScript, and beanshell all have Java-based interpreters. Therefore, code in these languages can run in the same JVM and process as Java applications.

Based on the above standards, the script interpreters involved in this evaluation include:

Jacl: JAVA Implementation of TCL.

Jython: JAVA Implementation of Python.

RHINO: JAVA Implementation of JavaScript.

Beanshell: a Java source code interpreter written in Java.

After specifying the types of interpreters to be compared, you can compare them from various aspects.

Iii. Evaluation: Availability

The first evaluation item is availability. This evaluation analyzes whether a certain interpreter is unavailable. Write a simple test program in each language and run it with the corresponding interpreter. The result shows that all the interpreters have passed the test, each interpreter can work stably or easily with it. Since each interpreter is worth considering, what factors may lead developers to favor one of them?

Jacl: If you want to create a user interface element in the TK script code, visit the swank project, which encapsulates the swing part of Java into TK. The release version does not contain the jacl script debugger.

Jython: supports scripts written in Python syntax. Python uses indentation layers to represent the structure of a code block, rather than using curly brackets or start-end symbols like many other languages to control the process. As for whether such a change is a good thing or a bad thing, it depends on your habits of users. The release version does not contain a debugger for Jython scripts.

RHINO: Many programmers always associate JavaScript with web page programming, but this version of JavaScript does not need to be run in a browser. I have not found any problems during use. Its release comes with a simple but practical script debugger.

Beanshell: Java programmers will soon feel a kind of kind to this source code interpreter. Beanshell documentation is well written, but the development team is very small. However, only when beanshell developers change their interests, but no one else fills the gaps left after their conversion interest, the Development Group is too small to become a problem. Its release version does not contain the beanshell script debugger.

Iv. Evaluation 2: Performance

The second evaluation item is performance. This test analyzes the speed at which each script interpreter executes some simple programs. This test does not require the interpreter to sort large arrays or perform complex mathematical calculations. Instead, it performs some simple and common operations, such as loop and integer comparison, and allocate and initialize large arrays and two-dimensional arrays. Testing procedures are simple, and these operations are more or less required for every commercial application. In addition, this test also analyzes the memory required for each interpreter to initialize and execute a simple script.

For the sake of consistency, the versions of each script language of the test program are as similar as possible. The test is performed on a toshba tecra 8100 notebook with a CPU of 256-MHz Pentium III processor and ram of MB. When JVM is called, The stack size uses the default value.

To facilitate understanding and comparison of the execution speed of script programs, this evaluation also runs Java programs with similar functions under Java 1.3.1, the TCL script written for the jacl script interpreter is also run in the Tcl local interpreter. Therefore, you can see the results of the two tests in the following table.

Table 1: For loop from 1 to 1000000:

Interpreter type time
Java 10 ms
TCL 1.4 seconds
Jacl, 140 seconds
Jython 1.2 seconds
Rhino 5 seconds
Beanshell 80 seconds

Table 2: compare whether integers are equal, 1000000 times:

Interpreter type time
Java 10 ms
TCL 2 seconds
Jacl, 300 seconds
Jython 4 seconds
Rhino 8 seconds
Beanshell 80 seconds

Table 3: allocate and initialize an array of 100000 elements:

Interpreter type time
Java 10 ms
TCL. 5 seconds
Jacl 25 seconds
Jython 1 second
Rhino 1.3 seconds
Beanshell 22 seconds

Table 4: allocate and initialize an array of 500x500 elements:

Interpreter type time
Java 20 ms
TCL 2 seconds
Jacl 45 seconds
Jython 1 second
Rhino 7 seconds
Beanshell 18 seconds

Table 5: memory required to initialize the interpreter in JVM:

Interpreter memory usage
Jacl about 1 MB
Jython about 2 MB
Rhino about 1 MB
Beanshell about 1 MB

This evaluation proves that Jython has the best performance, which is quite different from other interpreters. Rhino 2, beanshell is a little slow, and jacl is the bottom. However, for you, the impact of such performance data is closely related to the tasks you want to complete in script language. If a script function contains a large number of iterative operations, jacl or beanshell may be unacceptable. If there are few chances for the script program to execute code repeatedly, the speed difference between these interpreters is not that important. It is worth noting that Jython does not seem to provide built-in direct support for declaring two-dimensional arrays, but this problem can be solved through an "array" structure.

V. Evaluation 3: Difficulty of Integration

This evaluation includes two tasks. The first task is to compare the Code required to instantiate various language interpreters. The second task is to compile a script to complete the following operations: instantiate a Java jframe and put it into a jtree, adjust the size and display the jframe. Although these tasks are simple, we can see how much work is done by using an interpreter, and what the script code compiled for the interpreter looks like when calling a Java class.

■ Jacl

To integrate jacl into a Java application, first add the JAR file of jacl to the classpath of Java, and then create an instance of the jacl interpreter before executing the script. The following code creates a jacl interpreter instance:

import tcl.lang.*;

public class SimpleEmbedded {
public static void main(String args[]) {
try {
Interp interp = new Interp();
} catch (Exception e) {

The following jacl script code shows how to create a jtree, put it into a jframe, adjust the size, and display the jframe:

package require java
set mid [java::new javax.swing.JTree]
set f [java::new javax.swing.JFrame]
$f setSize 200 200
set layout [java::new java.awt.BorderLayout]
$f setLayout $layout
$f add $mid
$f show

■ Jython

To integrate Jython into a Java application, first add the JAR file of Jython to the classpath of Java, and then create an instance of Jython interpreter before executing the script. The code for completing this task is simple:

import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleEmbedded {
public static void main(String []args) throws PyException {
PythonInterpreter interp = new PythonInterpreter();

The following Jython script code shows how to create a jtree, put it into a jframe, and then display the jframe. The following code does not include the resize operation:

from pawt import swing
import java, sys
frame = swing.JFrame('Jython example', visible=1)
tree = swing.JTree()

■ Rhino

Like other interpreters, when integrating rhino, you must first add the JAR file of rhino to the Java classpath, and then create an instance of the rhino interpreter before executing the script:

import org.mozilla.javascript.*;

public class SimpleEmbedded {
public static void main(String args[]) {
Context cx = Context.enter();

The following simple rhino script shows how to create a jtree, place it into a jframe, adjust the size, and display the jframe:

frame = new Frame("JavaScript");
frame.setSize(new Dimension(200,200));
frame.setLayout(new BorderLayout());
t = new JTree();
frame.add(t, BorderLayout.CENTER);

■ Beanshell

Integrating beanshell is as easy as integrating other interpreters. Add the beanshell JAR file to the Java classpath, and create an instance of the beanshell interpreter before executing the script code:

import bsh.Interpreter;

public class SimpleEmbedded {
public static void main(String []args) throws bsh.EvalError {
Interpreter i = new Interpreter();

The following beanshell script code shows how to create a jtree, place it into a jframe, adjust the size, and display the jframe. The code is simple and has a familiar Java style:

frame = new JFrame();
tree = new JTree();

From the above descriptions, it is easy to integrate any interpreter in Java applications. At the same time, as long as you have mastered the syntax of the script language, you can efficiently compile and write the script program. The preceding simple examples show that scripts written with beanshell and JavaScript are the most similar in format to Java, while jacl and Jython are somewhat different, however, jacl and Jython scripts are not hard to understand. As shown in the above script for each script interpreter, there is no firewall between the script code and the Java application class. Therefore, you must note that the script code runs directly on the basis of the Java application class. Be sure that this is what you want. If you want to protect some parts of the application at runtime and prevent the script code from accessing some parts, you should take measures such as fuzzy processing of non-public code, this prevents people from directly programming undisclosed APIs.

6. Evaluation 4: Support and licensing issues

Although the integrated script interpreter gives the Java application additional capabilities, it also makes the Java application dependent on those script libraries. Before you decide to use a script interpreter, consider the chance that you must modify the integrated code one day in the future. This is not a good indication if the developer of the script interpreter rarely updates or upgrades the interpreter. It may mean that the interpreter implements the code perfectly at that time, or the developer responsible for the code has moved to other software projects. Which of the following situations is more likely? The answer is very obvious.

In addition, it is necessary to check the source code required to implement the interpreter. It is impractical to try to grasp every line of the interpreter code and extend or improve it, because the interpreter's code size is too large. Even so, it is still necessary to understand the scale of the interpreter, because in some cases, you may need to modify the source code of the interpreter, you may also need to have a deeper understanding of the interpreter code to understand the specific working principles of the interpreter.

Next let's take a look at the code library support problems for each interpreter.


Jacl has an active support and development group. Although the download link on the development website points to a release that was just a few years ago, the new version can be found through the CVS version control system. Jacl contains about 37000 lines of Java code.


Jython's support, maintenance, and updates seem very active. Jython contains approximately 55000 lines of Java code.


Rhino is updated and released frequently. It contains about 44000 lines of Java code.


Beanshell is also updated on a regular basis. It contains about 25000 lines of Java code, and many other beanshell scripts are provided.

We can see that all these interpreters are huge. If you can rely on interpreter development and support organizations to provide improvements and bug patches, you will have less trouble. Before selecting an interpreter, check whether the interpreter is upgraded and released frequently. Maybe you can contact a developer to learn about their long-term plan and bug fixing process.

These interpreters can be downloaded for free. However, what are their licensing rules if they are to be embedded into commercial applications? Fortunately, software licenses do not have any problems for all of these interpreters. Read the license protocols for jacl, Jython, JavaScript, and beanshell to find that you must follow the gnu lgp or equivalent license. This means that even if your Java application is not free, you can still include a script interpreter when releasing the application. However, you cannot delete the copyright information in the source and script files, but also explicitly tell the user that the script interpreter bundled with the Java application belongs to others.

VII. Conclusion

If you want to integrate Script Programming Support in Java applications, I suggest you select only one script interpreter. Adding a script to your product will incur a corresponding cost. Therefore, you should avoid integrating more than one script interpreter in Java applications. When adding script support for Java applications, using a Java-based interpreter instead of a local interpreter such as Perl can simplify future work and make your products more portable, it also facilitates the integration of Java programs and interpreters.

If you want to use a specific scripting language to customize your product, you must carefully check whether the problem occurs if the script interpreter that supports the language is integrated. If you don't have to be confined to a specific scripting language, you should compare the interpreters from multiple different perspectives to see which one is more suitable for the main tasks faced by Java applications.

For example, compared with other interpreters, jacl may be slow in development, but if you have to use Tcl scripts, it is still worthwhile to use jacl interpreters. If you want to port an application from Tcl/tk to Java, jacl enables the new Java application to run the original TCL script. The value of this capability may surpass other shortcomings. In addition, Tcl is a popular programming language and many developers are familiar with it, and books on Tcl programming are also easy to buy.

If you like Java-style script code and try to reduce the trouble in the integration process, beanshell looks good. Its disadvantage is that the beanshell syntax and programming user guide is only limited to the content contained in the release, and beanshell is slower than other script interpreters. On the other hand, I think beanshell is easier to use. Beanshell databases are well organized to simplify integration. If you choose the script interpreter, performance is not a key factor, you can consider beanshell.

Rhino runs faster than beanshell, and also supports Java-style scripts. In addition, it seems to have a high development quality and support services, and books on JavaScript syntax and programming are also easy to find. If you have almost a balance between performance, Java-style syntax, and powerful support services, rhino is undoubtedly recommended.

Among the four script interpreters evaluated in this article, Jython is the fastest and has some powerful programming functions. The only truly worrying thing is jyphon's process control syntax. However, you may or may not care about these differences in syntax. Just like jacl, because you need to learn a lot of new knowledge, writing scripts with Jython may take longer to learn than JavaScript and beanshell. If you want to write complicated scripts in Python, you should buy a book. Python is a popular programming language, so there are quite a few books to choose from.

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: 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.