University of Iowa OSUM
Sign Up

Mixing JRuby in Java

Sometimes you'll want to call JRuby directly from your Java program. Mixing a scripting language with the power of a more standard programming language has tremendous advantages. You can modify your program on demand, without studying up on advanced reflection topics. For example, a game developer might want to have objects that executive specific scripts when you activate them: scripts which could be stored in your database and easily updated by your developers. Instead of restarting your server to add new functionality or features to these objects, you can write the code directly in JRuby and have your program run it via whatever method you chose.

Calling JRuby from Java isn't exactly the most intuitive process. One method involves using the Java ScriptingEngines module, but the proper JRuby files can be hard to find and put into place. The easiest method I've found so far is, in fact, surprisingly easy.

Step by step how to get JRuby working inside Java:
  • Download and Install JRuby - You'll need files from the JRuby program, obviously. If you haven't already, you can follow my installing JRuby tutorial.
  • Find JRuby Jar Files - Go to your JRuby installation directory, and go to the lib folder. Now copy both bsf.jar and jruby.jar.
  • Add To JDK - Now you'll have to navigate to your Java installation. Go to java_path/jre/lib/ext and copy the files there.
Now it will work smoothly. You'll have to do this for any system to run the program, though, so be sure to install these files on whichever server or system you may have designed your application for. Now it's fairly simple. Congratulations, you should now be able to use JRuby within your Java applications. Copy and paste this code, and modify it to your needs:

import org.apache.bsf.*;

public class evalScript {
public static void main(String[] args) throws Exception {
// Create a script manager.
BSFManager bsfmanager=new BSFManager();

// Evaluate the ruby expression.
try {
} catch (BSFException exception) {


Resources Downloads


Link To Us