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:
Some well-known scripting languages are:
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.
There are two ways to use patrodyne-scripting-java-X.X.X.jar:
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:
System properties can be used to change the default location to search for additional source files, classes and jars:
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:
To run Java programs from the command line,
#!/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: