Page 4 of 5
import groovy.lang.GroovyShell;
import groovy.lang.Binding;
import groovy.lang.Closure;
import java.io.File;
public class ScriptRunner {
public static void main (String[] args) throws Exception {
GroovyShell interp = new GroovyShell(new Binding());
try {
File f = new File(args[0]);
interp.evaluate(f);
} catch(Exception e) {
System.out.println("Exception while sourcing file " + args[0]);
e.printStackTrace();
}
}
}
The Groovy script to create a JTree, put it in a JFrame, and show the JFrame looks like this:
import javax.swing.JFrame
import javax.swing.JTree
import javax.swing.WindowConstants
class SimplestGUI {
void buildIt() {
frame = new JFrame("Simplest GUI");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
tree = new JTree();
frame.getContentPane().add(tree);
frame.pack();
frame.show();
}
static void main(args) {
b = new SimplestGUI()
b.buildIt()
}
}
To integrate JudoScript into your Java application, you create a JudoEngine and then ask that JudoEngine to evaluate the file at the path specified:
import com.judoscript.JudoEngine;
public class ScriptRunner {
public static void main (String[] args) throws Exception {
JudoEngine je = new JudoEngine();
try {
je.runScript(args[0], args, null);
} catch(Exception e) {
System.out.println("Exception while sourcing file " + args[0]);
e.printStackTrace();
}
}
}
The JudoScript script to create a JTree, put it in a JFrame, and show the JFrame looks like this:
const #JFrame = java::javax.swing.JFrame;
const #JTree = java::javax.swing.JTree;
frame = new java::#JFrame('Simple JudoScript GUI');
tree = new java::#JTree();
frame.getContentPane().add(tree);
frame.pack();
frame.setVisible(true);
gui::events {
<frame : Window : windowClosing>: exit 0;
}
To integrate Pnuts into your Java application, you create a Context, then ask Pnuts to load the file at the path specified into that Context. Here's what the code looks like to make that happen:
import pnuts.lang.*;
import java.io.*;
public class ScriptRunner {
public static void main(String[] args) throws IOException {
try{
Context context = new Context();
Pnuts.loadFile(args[0], context);
} catch (Exception e) {
System.out.println(e);
}
}
}
The Pnuts script to create a JTree, put it in a JFrame, and show the JFrame looks like this:
import("javax.swing.JFrame")
import("javax.swing.JTree")
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
tree = new JTree();
frame.getContentPane().add(tree);
frame.pack();
frame.show();
To integrate JRuby into your Java application, you create an instance of Ruby and then ask it to load the file at the path specified:
import org.jruby.*;
import java.io.*;
public class ScriptRunner {
public static void main (String[] args) throws Exception {
Ruby runtime = org.jruby.Ruby.getDefaultInstance();
try {
File f = new File(args[0]);
runtime.loadFile(f, false);
} catch(Exception e) {
System.out.println("Exception while sourcing file " + args[0]);
e.printStackTrace();
}
}
}
The JRuby script to create a JTree, put it in a JFrame, and show the JFrame looks like this:
require 'java'
module JavaSwing
include_package "javax.swing"
include_package "java.awt.event"
end
frame = JavaSwing::JFrame.new("Simple Ruby App")
tree = JavaSwing::JTree.new()
frame.getContentPane().add(tree)
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
frame.pack()
frame.setVisible(true)
In the trivial example I used, you can see that integration is simple and that the different interpreters complete the tasks similarly. The scripts for the different languages are also similar. I think that you need to look at bigger, more complex examples than we have space for here to see the differences among the interpreters. To pick the best one for your application, you'll have to look deeper at the language syntax and feature set that the scripting interpreters support and decide for yourself if you like what you see.
In the process of doing these integrations, I took notes about what seemed most important, or interesting, or noteworthy about the different interpreters and have listed those notes below. Obviously, these thoughts represent my personal opinion. Software developers are free-thinking people, so I know you'll come to your own conclusions when you do your own due diligence.
Jacl supports the Tcl syntax, which is not difficult to learn. The Tcl programming language is already well known to many software developers, and numerous books and online tutorials are available on Tcl programming. One recommended interactive tutorial on Tcl programming is the TclTutor program, which takes you through the steps of learning to program in Tcl interactively.
JRuby supports the Ruby syntax. The RubyCentral homepage states that Ruby "combines the object-oriented power of the classic OO language Smalltalk with the expressiveness and convenience of a scripting language such as Perl." I wasn't familiar with Ruby, but the developerWorks article "Take a Shine to JRuby" (September 2004) gives you a taste of JRuby's strengths and shows a more elaborate JRuby programming example than I have in this article.
BeanShell 2.0 release notes state that BeanShell is now capable of interpreting ordinary Java source files, which is impressive. I tested this functionality and found it to work fine for the simple programs I asked it to load and run. If you want to learn more about BeanShell programming, check out the BeanShell tutorial on the BeanShell Website.
Jython supports the Python syntax. If you are unfamiliar with Python and don't have one of the Jython books handy, one place to
start learning about Python is with Guido van Rossum's Python tutorial. One feature of the Python syntax is that it doesn't use braces to group statements together, it uses a combination of the
colon character (:) and space indentation. This might seem like it could lead to confusion in the code, but consistent usage of spaces instead
of tabs is all that's required to keep things clear. If you are looking for in-depth programming advice on Jython, several
good books are available.
Archived Discussions (Read only)