Programming Tools: Java Scripting Languages
I recently returned from JavaOne 2005 in San Francisco. The show was impressive for a number of reasons. The attendance seemed to be about 30% larger than last year's. The same could be said for the number of tutorials, sessions and BOFs. For example, there were enough BOFs to run until 11:00pm at night. Many of the sessions were filled to capacity, with over 600 attendees each technical presentation.
Given my strong background in C++, I am used to a more amorphous attitude toward languages. Therefore, I was surprised to see that there still is a vibrancy to Java that I do not see with C++.
Among the many interesting things that I saw, two stand out: Eclipse and the scripting language called Groovy. I already have written about Eclipse, so here I simply note that Eclipse now seems to dominate the Java IDE world. Groovy, on the other hand, is new. It prompted me to look at Python-like scripting languages that run in a Java environment.
In this article, I discuss Jython and Groovy, two scripting languages that use the Java runtime environment. By the way, JavaScript has nothing to do with the Java language, so it is not considered here.
Jython is an interpreter for the Python language. It is written in Java to run under the Java VM. To handle Python built-in entities, Jython produces Java compatible structures that it passes back and forth to the JVM. The code below uses the Jython interactive shell, in which Jython takes the Python built-in list structure and converts it to a Java list structure that is then passed to the Java println function that then passes the result to the JVM.
>>> print ['a','list','of','strings'] ['a', 'list', 'of', 'strings'] >>>
Here is a simple program that uses the Java Swing library to produce a trivial GUI application:
"""\
A simple demonstration of creating a swing tree widget from a
Python dictionary.
"""
import java
import javax
from pawt import swing
from pawt import awt
sampleData = {
'PyObject': {
'PyInteger':None,
'PyFloat':None,
'PyComplex':None,
'PySequence': {
'PyArray':None,
'PyList':None,
'PyTuple':None,
'PyString':None,
},
'PyClass': {
'PyJavaClass':None,
},
},
'sys':None,
'Py':None,
'PyException':None,
'__builtin__':None,
'ThreadState':None,
}
Node = swing.tree.DefaultMutableTreeNode
def addNode(tree, key, value):
node = Node(key)
tree.add(node)
if value is not None:
addLeaves(node, value.items())
def addLeaves(node, items):
items.sort()
for key, value in items:
addNode(node, key, value)
def makeTree(name, data):
tree = Node("Sample Tree")
addLeaves(tree, data.items())
return swing.JTree(tree)
def exit(e):
java.lang.System.exit(0)
if __name__ == '__main__':
tree = makeTree('Some JPython Classes', sampleData)
button = swing.JButton('Close Me!', actionPerformed=exit)
f = swing.JFrame("GridBag Layout Example");
p = swing.JFrame.getContentPane(f)
gb = awt.GridBagLayout()
p.setLayout(gb)
c = awt.GridBagConstraints();
c.weightx = 1.0;
c.fill = awt.GridBagConstraints.BOTH;
gb.setConstraints(tree, c);
c.gridx = 0;
c.gridy = awt.GridBagConstraints.RELATIVE;
gb.setConstraints(button, c);
p.add(tree)
p.add(button)
f.pack();
f.setSize(f.getPreferredSize());
f.show();
Jython has two main advantages. First, the language is Python and has all of its strengths, including clean and consistent syntax, introspection, dynamic creation of classes, properties and attributes. Second, it runs at the speed of the JVM. On the other hand, Jython also has two chief weakness. First is its need to convert constantly between Python and Java structures. Second, Jython has not received much work in recent years. That is about to change, however; the current maintainer, Brian Zimmer, recently received three grants to continue work on Jython.
The bottom line is if you know Python, then you know Jython. The big gain is ready access to libraries such as Swing, awt and so on.
Groovy is a new language based on features from Ruby, Python and Haskell. However, its runtime environment is any JVM. The language syntax attempts to be Java-like, while making the form of the language much simpler. Groovy is a statically typed language, so it requires a compile step before producing Java byte code. Groovy does not currently have an interpreter, although it does have a shell.
Groovy documentation offers an impressive list of features:
Closure support
Native syntax for Lists and Maps
Groovy Markup
Groovy Path expression language
Groovlets for implementing Servlets easily in simple Groovy scripts
Groovy SQL for making SQL more Groovy
Groovy Beans for simpler syntax for working with beans
Groovy Template Engines which are pluggable, simple to use, integrate GPath and compile to bytecode
Ant scripting
Regex syntax for neater scripting with regular expressions
Operator Overloading to simplify working with datatypes Collections and Maps
Polymorphic iteration and autoboxing
Compiles straight to Java bytecode
Works cleanly with all existing Java objects and libraries
An interesting feature of Groovy is found its definition and use of closure:
A closure in Groovy is an anonymous chunk of code surrounded by braces that takes zero, one or more arguments, returns a value, and can reference and use variables declared in its surrounding lexical scope (i.e., the scope at its definition point). A closure is like an anonymous inner class in Java. It is often used in the same way. However, Groovy closures are more powerful and often more convenient to specify and use.
Groovy's uses Java's standard types for things such as dictionaries and lists. This means no translation is needed between Groovy code and the JVM. This may improve execution time, depending on the application.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
3 hours 17 min ago - Nice article, thanks for the
13 hours 58 min ago - I once had a better way I
19 hours 44 min ago - Not only you I too assumed
20 hours 1 min ago - another very interesting
21 hours 54 min ago - Reply to comment | Linux Journal
23 hours 48 min ago - Reply to comment | Linux Journal
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago - Favorite (and easily brute-forced) pw's
1 day 8 hours ago - Have you tried Boxen? It's a
1 day 14 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
a sign of dynamic typing is
a sign of dynamic typing is the following (groovy code):
def foo(String s) {"String"}
def foo(Object o) {"Object"}
def obj = "obj"
assert foo(obj) == "String"
Object obj2 = "obj"
assert foo(obj2) == "String"
both, obj and obj2 have the static type java.lang.Object when viewed from Java, but for the function call the type String is used, as this is the runtime/dynamic type. So Groovy does have dynamic typing.
A compile step is not a sign of the typing mechanism.
What about BeanShell?
Sometimes I wonder whether I am the only person in the world who uses BeanShell, It's a java interpreter written in java and uses java syntax (have I written "java" too many times? ;) .)
I think there are two nice advantages when using BeanShell:
1) The programmer does not have to learn new syntax.
2) If desired, a more script-like syntax can be used.
Also BeanShell scripts can have acess to every object within the host application, can acess all of the available java classes and runs at the VM speed (it just means I like it a lot :) .)
I am not trying to make a case for BeanShell, I'm just curious to know why BeanShell has not gotten more coverage in "Java Scripting Languages" articles and learn more about the comparative advantages of using a diferent approach to java scripting.
JavaScript
By the way, JavaScript has nothing to do with the Java language, so it is not considered here.
Historically, this statement is correct, but that is about to change. Mazilla has a javascript implementation in Java called Rhino that is slated to be included in the next major release of the JVM. And we had just convinced everyone that Java and JavaScript are not the same....
NetRexx
Nice Article! I couldn't help but think about NetRexx which is Rexx built for a JVM. Rexx is an old classic scripting language from mainframes, OS/2, Amiga, Unix (sic!). It would be nice to also hear about Rexx's uses in this context, i.e. JVM scripting languages!
Ciao!
Good article. About your ver
Good article. About your very last sentence, if someone wants to actually influence development and design decisions, go with groovy. Jython isn't going to be influenced by anyone, because it is meant to be an exact clone of CPython, nothing more, nothing less (same way with IronPython for .NET).
For those comfortable with statically typed languages, scala (http://scala.epfl.ch/) and nice (http://nice.sf.net/) have some really fascinating features.
Good article; a few typos
I enjoyed the article. I had heard of Groovy, but not seen any examples. While the examples shown here are small, they do at least provide a little bit of flavor as to what the language is like.
There are some typos, however. In the section on Closures, there is obviously some markup intermixed in the first code example. Then in the immediately following paragraph, the word "each" should have double quotes around it, but only the first one is there.