A Scripting Engine for Java

Scripting is the Java API consisting of interfaces and classes that implement JSR-223 and provide a framework using runtime engines in your applications.

The main areas of functionality of the javax.script package include:

  • Script execution - Scripts are text sources for programs executed by script engines.
  • Binding - Allows Java objects to be exposed to script programs as named variables.
  • Compilation - Allows the intermediate code of a script engine to be stored and executed repeatedly.
  • Invocation - Allows the reuse of intermediate code generated by a script engine's front-end.
  • Script Engine Discovery - Script engines are packaged to be discovered at runtime and queried for attributes.

Some well-known scripting languages are:

  • Bean Shell - Executes Java syntax and extends it with common scripting conveniences.
  • Groovy - Builds upon the strengths of Java but has additional power features inspired by Python, Ruby and Smalltalk.
  • JavaScript - A multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.
  • Jython - A Java implementation of Python. It is a general-purpose, high-level programming language with code readability.

Missing from the above is a scripting engine for Java code, itself. That is what this project provides, a script engine to execute Java code from Java, dynamically. Actually, the reference implementation for JSR-223 provides the basis for a Java Scripting Engine but it is not easy to find and the code is a wee bit dated. This project is a modification of the RI version and is packaged for easy use in your applications or as a stand-alone runner for simple Java programs.

Usage

There are two ways to use patrodyne-scripting-java-X.X.X.jar:

  1. Direct - Execute the jar from the command line to run a Java class.
  2. Embedded - Include the jar in a Scripting enabled application.

Direct

The JAR for this project can be executed directly:

Linux:

    java -jar patrodyne-scripting-java-X.X.X.jar $*

Windows:

    java -jar patrodyne-scripting-java-X.X.X.jar %*

Example:

    java -jar patrodyne-scripting-java-X.X.X.jar HelloWorld.java arg0 arg1 arg2

In the above examples, the JAR and the Java sources must be in the same directory. You can apply your programming skills to increase flexibility, just add a fully qualify the location to the JAR.

Note: The java command must be on your path!

Linux:

    java -jar ${HOME}/lib/patrodyne-scripting-java-X.X.X.jar $*

Windows:

    java -jar %HOME%\lib\patrodyne-scripting-java-X.X.X.jar %*

By specifying the JAR with a fully qualified path, you can run a Java source file from the current working directory. A source file, say HelloWorld.java must contain the usual 'public static void main(String[] args)' method before for your program will do anything useful. When a main method is found, the arguments after the file name are passed into your program, as you would expect.

Here is an example of HelloWorld.java:

import javax.script.*;

class HelloWorld
{  
    private static ScriptContext ctx;
    public static void setScriptContext(ScriptContext ctx)
    {
        this.ctx = ctx;
    }

    public static void main(String[] args)
    {
        System.out.println("Hello World!");
        for (int argno=0;  argno < args.length; ++argno)
            System.out.println("\t"+argno+": "+args[argno]);
    }
}

When this example is evaluated as a JSR-223 script by our JAR, the ScriptContext is set prior to execution of the main method. The script context can contain runtime objects as provided by the script engine. In fact, in direct mode, the context will always contain:

  • an object named javax.script.filename to provide the source file name.
  • an object named arguments to represent the command line options as an array of strings, in the usual way.

System properties can be used to change the default location to search for additional source files, classes and jars:

  • -Dorg.patrodyne.scripting.java.sourcepath - colon separated list of paths.
  • -Dorg.patrodyne.scripting.java.classpath - colon separated list of paths, jars, etc.

Embedded

The second way to use our Script Engine for Java is to include it in the class path of a JSR-223 enabled application. An excellent example of such an application is Patrodyne's TransformIO.

Metadata:

  • Engine Name: JavaCode
  • Short Names: JavaCode, Java
  • MIME Type: application/java
  • Extensions: java

Utility

To run Java programs from the command line,

  1. put patrodyne-scripting-java-X.X.X.jar into your ~/lib path,
  2. add this script to your ~/bin path,
  3. name it psj.
#!/bin/sh
# Patrodyne's Scripting Engine for Java
java -Dorg.patrodyne.scripting.java.classpath="." \
  -jar ~/lib/patrodyne-scripting-java-X.X.X.jar $*
# vi:set tabstop=4 hardtabs=4 shiftwidth=4: