Home > Software > Scripting-enabled applications

Scripting-enabled applications


First of all, why in the hell you would like to do this? Yeah, sure, if you develop an IDE or a word processor makes sense, but otherwise … ? No, no, no, you got it all wrong. This will help even for that small weekend fun application that you write in your spare. Either you develop a small testing console, either you want your application to record and run some macros (quite useful, isn’t it) or even a little bit more complex configuration file, scripting will come in very handy for you. And you don’t want to develop a new language for several reasons: it takes quite a while to write a parser and an interpreter, maybe your users wouldn’t like to learn a new language and … there are already too many scripting languages out there.
And what if you decide in half an year that JavaScript it’s not the thing for you and you want Ruby? Or, why not, both?

Fortunately, Sun also acknowledged the need for this nowadays and included it in J2SE 6.
Before, it was BSF, but Java 6 support for scripting is considerably improved so I will not insist on some history lessons. Actually what I want to do is show you how to do this as fast as possible.

As a piece of code worths a thousand of words


ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");
Bindings bindings = scriptEngine.createBindings();
StringBuffer sb = new StringBuffer("Hello");
bindings.put("s", sb);
try {
scriptEngine.eval("s.append(' world');", bindings);
} catch (ScriptException e) {
e.printStackTrace();
}
System.out.println(sb);

Wow! That’s it? Yeap, that’s pretty much what you have to do to embed scripting in your application.

Ok, now let’s discuss it a little bit more in detail.

First of all you have to instantiate a script engine based on the language you want to use

ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");

js is only a short name for JavaScript. You can also instantiate a script engine based on a file extension or a mime type (see ScriptEngineManager).

Then you have to create and populate the bindings. These are objects that you can refer from your scripts using a given name.

Bindings bindings = scriptEngine.createBindings();
StringBuffer sb = new StringBuffer("Hello");
bindings.put("s", sb);

Here we will have only one such object that can be referred through name s and which is actually a StringBuffer containing Hello.

And finally invoke your script:

try {
scriptEngine.eval("s.append(' world');", bindings);
} catch (ScriptException e) {
e.printStackTrace();
}
System.out.println(sb);

In this case was a simple testing script, but you can actually run scripts from files, URLs etc.

And if you want to run your scripts with more than a few bindings defined, you can use the ScriptContext and even redirecting the standard input, output and error of your scripts.

Simple enough, right?

And not even the installation is more difficult. You have everything in your JDK 6. And if you want more scripting languages, just download the implementation and drop it in your classpath. The Java scripting API has a nice discovery mechanism and you will have them automatically set up. E.g. by adding the BSF (mentioned earlier) jar in your classpath you can use all its languages.

Try the below to list all the scripting languages installed in your system:

for (ScriptEngineFactory sef : new ScriptEngineManager().getEngineFactories()) {
System.out.println(sef.getEngineName());
}

And if we mention that you can even compile a script and in IDEs like IntelliJ and Eclipse you even have debug support for the major scripting languages through plugins, let’s hear people complaining that Java is only Java!

About these ads
Categories: Software Tags: , ,
  1. Ivana
    February 6, 2008 at 10:51 pm | #1

    Hi,
    I somehow googled your blog and I would like to ask one thing. In script, i can call methods on java objects, but i cannot call methods on my custom objects ? Is that true ? Because I bumped in this problem and i cannot find good alternative solution. thank you very much

  2. February 8, 2008 at 11:41 am | #2

    What do you mean by custom objects? If you’re referring to your own Java objects then you should be able to do it. Please remember that you have to declare your methods public.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: