SimpleScript

August 4th, 2010 Leave a comment Go to comments

SimpleScript – Script engines of Java

SimpleScript provides a necessary structure for creating script engine usable in Java. There are currently two example engines: Java and JavaScript.

Currently available engine

Java as Script

Using JavaCompiler and SimpleScript, a script engine for Java language can be created. Users can create a file containing a Java code and execute it as a script. The code will be compiled automatically by JavaCompiler. The compiled byte-code is appended the script file as text so that it can be loaded (instead of recompiled) in the future. SimpleScript ensures the integrity of the compiled code. If the code or the compiled code or both are tampered, SimpleScript will recompile the code before executing it.

Since the Java as script code is compiled to byte code, it is executed as a regular Java code. Therefore, there is zero penalty in term of performance during the execution. Only performance penalty is when the code is loaded.

JavaScript as Script

Using the Scripting for Java Platform (JSR 223), a simple script engine can JSR223 script engine can be created. During the time of the development, only JavaScript JSR-223 engine (Rhino) was available. JavaScript SimpleScript engine was created to see if JSR223 can be used with SimpleScript.

An example script

Let say we have a script file “test.ss“:

// ## This line will be ignored as it ends with "##" .                       ##
// ## This line will be also ignored as it also ends with "##".              ##
// ## The next line specifies that this is a Java main script.               ##
// @Java: { main }

// == Import area =============================================================
import java.io.*;

// == Here is a local elements ================================================
static  int I = 0;
private void Println(Object pObj) {
    System.out.println(pObj);
}
private void Println() {
    System.out.println();
}

// == Here is the Annotation for this code (main) =============================
@SuppressWarnings("unchecked");

// == The program start here ==================================================

// Here is the "Hello World!!!"
Println("Hello World!!!");
Println();

// Access to arguments
Println"Main Args = " + Arrays.toString($Args));
Println();

// Static field
Println("static int I = " + I);
Println();

// Classes in other packages
Println("Working Dir: '" + (new File("'.")).getAbsolutePath());
Println();

Now, you can run the code using.

SCRIPTPATH=/path/to/test/dot/ss/
TOOLSPATH=/path/to/jdk6-or-up/tools.jar
java -classpath $TOOLSPATH:$SCRIPTPATH -jar SimpleScript-bin.jar --run test.ss arg0 arg1

OR you can call it from Java as:

// Here is a regular Java code - assume "import net.nawaman.script.*;"
Function Test = (Function)Tools.Use("test.ss");
Test.run("arg0", "arg1");

The result would be:

Hello World!!!

Main Args = [arg0, arg1]

static int I = 0

Working Dir: '/path/to/the/working/directory/'.

As you can see, there is no explicit compilation. The script was just executed.

You can even execute a Java command via command line without having a script file. Like this:

java -classpath $TOOLSPATH:$SCRIPTPATH -jar SimpleScript-bin.jar  --command "/* @Java: */ return 5 + 10;"

Return:

15

You can also use JavaScript:

java -classpath $TOOLSPATH:$SCRIPTPATH -jar SimpleScript-bin.jar  --command "/* @JavaScript: */ 5 + 10;"

Return:

15

As you can see, SimpleScript can be very handy. It can be used as quick runs, test cases, experimentation code, smart configurations and others.

Download and Installation

  1. Install J2SE6 SDK or higher: SimpleScript requires J2SE6′s javac tool.
  2. Download: Get the binary and examples of SimpleScript here.
  3. Locate J2SE6 SDK’s Tools.jar: For Ubuntu Linux with Sub’s J2SE standard installation, the path is ‘/usr/lib/jvm/java-6-sun-1.6.0.20/lib/tools.jar‘.
  4. Script script: Create a simple bash(or batch for DOS) script to make your life easier. For Example: ss bash script:
    #!/bin/bash
    
    TOOLSPATH='/usr/lib/jvm/java-6-sun-1.6.0.20/lib/tools.jar'
    
    SCRIPTPATH='.'
    
    java -classpath $TOOLSPATH:$SCRIPTPATH -jar SimpleScript-bin.jar "$@"

Done!!! You can now just run ss –run test.ss arg0 arg1 arg2.

More information and examples

SimpleScript can do lots of things both via command line and with API. I will post them later in my blog.

  1. Introduction …
  2. Installation …
  3. Simple Examples …
  4. Simple Structure …

Copyright 2010: Nawapunth Manusitthipol