Scripting Support in Mustang - An Example You Can Try

Guest Author

Well, it's 1:30 a.m. but feels like late afternoon. Time shift is a funny thing. I thought that if I can't sleep, I could try to play with the new scripting support in Mustang (JSR-223). Well, it's as simple as this:

Preparing JavaScript code (notice I am using java.lang.StringBuffer inside JavaScript - isn't that cool?):

        input.setText("var out = new java.lang.StringBuffer();\\nfor (i=0; i<3; i++)\\n   
out.append('hello, world! ('+i+')\\\\n');");

Executing the code:

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine jsengine = manager.getEngineByName("js");
try {
String out = new String((StringBuffer)jsengine.get("out"));
} catch (javax.script.ScriptException e) {
output.setText("ScriptException" + e);

A screenshot:

Full source code is here. To execute, you need to install latest mustang build.

Well, if it's simple as this it may mean future Java code getting "polluted" by scripting code. Programming in scripting languages can be very productive for certain types of problems. Currently, Rhino JavaScript support is available by Sun as reference implementation and PHP support is being written, too. Any other languages may follow - the scripting framework enables any other languages to be plugged in. I really wonder what will be the impact of JSR 223 on web development...

Join the discussion

Comments ( 2 )
  • Vincent Brabant Wednesday, July 6, 2005
    You can also use zamples.com, in place of installing last version of JDK.
    You can copy that code in the window of Zamples, and see result on your right:
    public class Test {
    public static void main(String argv[]) {
    String x = "var out = new java.lang.StringBuffer();\\nfor (i=0; i<3; i++)\\nout.append('hello, world! ('+i+')\\\\n');";
    javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager();

    javax.script.ScriptEngine jsengine = manager.getEngineByName("js");
    try {
    String out = new String((StringBuffer)jsengine.get("out"));
    } catch (javax.script.ScriptException e) {
    System.out.println("ScriptException" + e);
  • Roman Strobl Wednesday, July 6, 2005
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.