Java Reference
In-Depth Information
generally quite a bit faster (the Java interpreter is already up and running, so why wait for an-
other copy of it to start up?). See Chapter 22 .
When building industrial-strength applications, note the cautionary remarks in the Java API
docs for the Process class concerning the danger of losing some of the I/O due to insuffi-
cient buffering by the operating system.
Calling Other Languages via javax.script
Problem
You want to invoke a script written in some other language from within your Java program,
running in the JVM, with the ability to pass variables directly to/from the other language.
Solution
If the script you want is written in any of the two-dozen-plus supported languages, just use
javax.script . Languages include awk, perl, python, Ruby, BeanShell, PNuts, Ksh/Bash, R
(“Renjin”), several implementations of JavaScript, and more.
Discussion
Example 24-6 is a very simple demo where we know the name of the scripting engine we
want to use. R is a well-known statistical computing/scripting language, itself cloned from
Bell Labs' “S” language. Renjin is a pure-Java reimplementation of R.
Example 24-6. RenjinScripting.java
/**
* Demonstrate interacting with the "R" implementation called "Renjin"
*/
public
public static
static void
void main ( String [] args ) throws
throws ScriptException {
ScriptEngineManager manager = new
new ScriptEngineManager ();
ScriptEngine engine = manager . getEngineByName ( "Renjin" );
engine . put ( "a" , 42 );
Object ret = engine . eval ( "b <- 2; a*b" );
System . out . println ( ret );
}
Search WWH ::




Custom Search