Scripting in Java
Wednesday, August 18, 2010 11:04:27 PM
If you want to add scripting support to your Java application you would want to use any that support the JSR 233 specification.
Rhino is included in the jvm if you use Java 6 so you can use it directly, without any extra jars in your classpath.
The following example uses 3 scripting languages (javascript, ruby and python) that all do the same: add items to a bag and print them to the console.
Inside the scripts you'll see the bag variable, which is put using the engine's API and how to import and use a Java's class inside them.
In order to switch from one scripting language to another you have to pass the name as argument.
You can test the example including the files into a project and running the ScriptingTest class. You'll need python.jar from the Jython installation and JRuby's complete jar.
The scripts files (test.js, test.rb and test.py) has to be in the root of your classpath.
Downloads:
java-scripting.zip
Rhino is included in the jvm if you use Java 6 so you can use it directly, without any extra jars in your classpath.
The following example uses 3 scripting languages (javascript, ruby and python) that all do the same: add items to a bag and print them to the console.
Inside the scripts you'll see the bag variable, which is put using the engine's API and how to import and use a Java's class inside them.
In order to switch from one scripting language to another you have to pass the name as argument.
You can test the example including the files into a project and running the ScriptingTest class. You'll need python.jar from the Jython installation and JRuby's complete jar.
The scripts files (test.js, test.rb and test.py) has to be in the root of your classpath.
Downloads:
java-scripting.zip






