You??™ll find that this code is much easier to maintain, though, and when the
JRuby engine changes its interface (which it will do, over and over again), you won??™t have to
CHAPTER 6 ?– JAVA INTEGRATION 113
change your own code to accommodate. BSF takes care of that. Also, if you would like to add
another script engine for some reason, it will be easy to include it in the current setup,
because BSF handles one language much like the other.
JSR223??”Java Scripting
The superior way of running JRuby from your own application is through the Java Scripting
API, also known as JSR223. It became a standard part in Java with the Java 6 SE release. The
goals of JSR223 are pretty much the same as those of BSF. Simply enough, you should be able
to use a scripting language with a standard Java installation, without any extra libraries except
those for the engine of the language you??™re interested in. Java bundles a JavaScript engine, and
will probably bundle more languages in the future.
The usage of Java Scripting is similar to BSF. You need to have Java 6 installed, and you
need to download the JSR223 Engines package from its home page
(http://scripting.dev.java.net). After you??™ve done that, you should unpack it, and add the
file jruby/lib/jruby-engine.jar to your CLASSPATH. With all that done, you should be set to go.
package c6;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class JRubyJSR223 {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.
Pages:
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204