Running scripting language in Java

Last week while I was working on an application which needed the maximum use of Scripting language, a thing just stroked in my mind, can’t we run scripts directly in different languages????

On lot of searching, I just came up with the solution of using scripting language like JavaScript, python, NetRexx, TCL etc. within the java language with utmost ease.

Java 6 introduces scripting engine support in to Java, and this new support provides a link between the Java and delayed binding world of scripting languages.

This all is possible with a simple package that java provides us. The Java Scripting functionality is in the javax.script package. This is a relatively small, simple API. Scripts can be executed from within java in the JVM. Many independent scripting packages have been available for Java for some time, including Rhino, Jacl, Jython, BeanShell, JRuby, and others. To work with scripts, you only need to know a couple of key classes, form which here we will learn two of them:

1)     ScriptEngineManager

2)     ScriptEngine

The ScriptEngine interface models script engines, and the ScriptEngineManager class provides convenient methods to register and obtain a ScriptEngine object.


To instantiate a script engine, you use the ScriptEngineManagerclass to retrieve a ScriptEngine object for the scripting language you’re interested in. Each scripting language has a name like JavaScript, rhino etc.

Generally it handles two key functions:

i.            Discovery of script engine

ii.            Storing data in context

It provides a key value pair, which is provided to all the instances of ScriptEngine created by the ScriptEngineManager and it is referred as “Global state”.


ScriptEngine is an interface which handles script interpreting and evaluation. This is implemented separately for every single language. Apache commons provides a project Jakarta Bean Scripting Framework (BSF) which gives implementation for a several set of scripting languages like TCL, NetRexx, JavaScript, Python lot more…

Let’s take an example using JavaScript as a scripting language:

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;

public class ScriptDemo {

public static void main(String[] args) {

// create a ScriptEngineManager object

ScriptEngineManager manager = new ScriptEngineManager();

// get the ScriptEngine instance

ScriptEngine engine = manager.getEngineByName("javascript");

// execute script

String script = "print('This is an example to run JavaScript in java')";

try {


engine.eval("var a= 10;");

engine.eval("var b = 10;");

engine.eval("var c = a + b;");

engine.eval("print (c);");

} catch (ScriptException e) {





The output of the program will be:






For more information on scripting packages, its variables, API and various examples to work with you can visit Java Scripting guide.

So if you find a problem like mine and hopefully you come across this article, I would be grateful helping you with your queries🙂

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: