New to the JDK 6.0 is support for scripting languages such as JavaScript. One advantage that Cay Horstmann is looking for is the ability to quickly experiment with code fragments. In his blog, Cay describes a few nifty things that he did that demonstrate just how far the new support for JavaScript goes.
Today, I discovered the JavaScript console in JDK6. From a command shell, run jrunscript (assuming, of course, that you have $JAVA_HOME/bin on your path). You get a JavaScript interpreter that lets you script Java classes.
The first thing that Cay wrote was a little "Hello World" Swing app. As he points out, this simple task was completed without the usual declarations that would be needed for a full blown Java Swing application. His next experiment was one that most of us struggle with at times, getting the right sequence of cryptic symbols needed to create a regular expression. It was at this point that things started to break down. First problem is that the call to split() method needed to be setup differently. Though his first attempt at splitting a string worked, subsequent attempts using different expressions did not yield the expected results. js> Arrays.asList("foo bar".split("
s+")); [foo bar] When you code test this code fragment in pure Java you get the expected answer [foo, bar]. This lead Cay to speculate that maybe a JavaScript string isn't the same as a Java string. Changing the code so that "foo bar" was a Java string was the trick needed to get it done. Cay's conclusions that he wasn't left with a "warm and fuzzy" feeling that the JavaScript console will be helpful. So question remains; is this tool a valuable addition to the JDK distribuion?