# Extend Java app with scripting and configuration

This topic is 2127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a tool written in Perl, about 2600 lines of code.
The tool is used to parse XML files and genrate C++ source based on certain rules.
I'm going to rewrite the tool in Java because I'm expecting the tool will become quite complicated in the future and Perl code maybe hard to maintain (no need to debate on this please).

The reason I used Perl is then I can use simple Perl script as configuration and scripting.
The tool needs a lot of configuration and also that it needs to allow the user to handle certain events in script.

My question is, I want to hear your advice on how to do scripting, or which script language to use in Java?

My requrements:

1. Lua, Perl, and Javascript are most prefered. Python is OK. Any other simple scripts that only require 10 minutes to learn are also OK but not prefered. The tool only needs one scripting language, not all the scripts mentioned above.
2. Java only. No need native executable. This is negotiatable but I highly prefer this point.
3. The script can handle and modify Java objects.
4. Easy to integrate to my tool. I don't want to spend one day on compiling the script binding library.
5. Native regular expression support in the script. Not a must but a hope.

I googled and found several candidates, but I'm not sure which one is best.
I think a lot of folks here had extended Java with scripting in your tools, so I would like to hear from you.

Thanks

Just to help you understand, below is a Perl script as the config. The user just passes its file name then the tool will load it and run it as Perl code.

 \$config = { title => 'blah blah', autoRegister => 1, outputCallback => \&processCallback, }; sub processCallback { # Perl code here to handle the callback }  Edited by wqking

##### Share on other sites
My question is, I want to hear your advice on how to do scripting, or which script language to use in Java?[/quote]
Unlike C/C++, Java - and .Net - is less of a language and more of a platform via the JVM. Thus, there are an extremely large number of JVM languages that can interpolate with Java sans any explicit binding.

Lua, Perl, and Javascript are most prefered. Python is OK. Any other simple scripts that only require 10 minutes to learn are also OK but not prefered. The tool only needs one scripting language, not all the scripts mentioned above.[/quote]
As far as I am aware, both Lua (LuaJ, et. al) and Perl (JPL) have bindings but aren't overly popular (I've never used either myself nor have I seen any questions regarding them). Rhino is the Javascript implementation in Java (and it is quite fast) and Jython is the Python version.

The script can handle and modify Java objects.[/quote]
Most of them do it natively (e.g. there are no explicit bindings) courtesy of the JVM. As a side effect, they also have access to all of Java's libraries.

Java only. No need native executable. This is negotiatable but I highly prefer this point.[/quote]
Most of them are fully implemented in Java or have the natives buried in an archive and you never need to mess with it.

Easy to integrate to my tool. I don't want to spend one day on compiling the script binding library.[/quote]
That's more of a design problem as "easy to integrate" depends on your tool. That said, working with scripting languages in .Net or Java is nothing like dealing with the same in C/C++.

Native regular expression support in the script. Not a must but a hope[/quote]
A lot of them do, and the ones that don't can simply piggy-back Java's implementation if need be.

I googled and found several candidates, but I'm not sure which one is best.[/quote]
"Best" is relative to your project's needs. If you're looking for simple, then Rhino is an excellent choice (just make sure you download Mozilla's SDK and avoid the butchered abomination included in the Java distribution) as it provides a nice balance of speed and flexibility. If you want something more substantial then JRuby or any of the other myriad of OO oriented languages might be appropriate. For raw speed Scala or Groovy++ (not dynamic Groovy) is as fast as they come.

The nice thing about platforms like .Net and Java is that your scripting language choice is more about personal preference and less about which language's bindings will include the least amount of suffering in order to get operational. Download the archives, plop them on your classpath, and be productive.

##### Share on other sites
Update, I just tested Rhino for a while. It works perfect for me and meets all my requirement.
Even better, I don't need to compile it. There is a JAR with only 1M file size there.
Very good.