Friday, November 9, 2012

BeanShell: The Java Interpreter (JSR-274)

BeanShell is a pure Java interpreter. BeanShell uses BeanShell Scripting Language which is a Java syntax compatible scripting language for the Java platform (JSR-274). BeanShell supports dynamic execution of the full Java grammar and semantics with transparent access to Java objects and APIs. It also provides additional scripting features which is a strict superset of the Java language syntax.

Setting up BeanShell
1. Download latest BeanShell jar file. The current one is bsh-2.0b4.jar.
2. Place the jar file in a directory say D:\env\javaLibs\.
3. Set CLASSPATH environment variable pointing to BeanShell jar file. It will look like .;D:\env\javaLibs\bsh-2.0b4.jar; under Windows. The . is to look for classes in the current directory. If the variable is already present, append the path at the end separating the paths with a semi-colon.
4. Open command prompt and run java bsh.Interpreter (assuming that JDK or JRE is present in the path).

Now we have a fully working Java interpreter which we can use to quickly prototype ideas. The interpreter is similar to what we see in other dynamic languages like Python or Groovy.