How to use GEM from Java application

I am newbie to JRuby. Most of our development is done as Java App, but
for some features we would like to use JRuby scripting. I was able to
get standard RUBY script to run from our App, but when I try to perform
require of GEM, I am getting error.

For e.g., If I have simple Ruby file like test.rb, the following Java
code runs fine after I add jruby.jar as external JAR in my Java project.


puts “Hello World!!!”


package test;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.jruby.embed.PathType;

public class BsfLoadPathSample {
private final static String jrubyhome = “c:/jruby”;
private final String filename = jrubyhome + “/samples/test.rb”;

private BsfLoadPathSample() throws BSFException, FileNotFoundException
“org.jruby.embed.bsf.JRubyEngine”, new String[] {“rb”});
BSFManager manager = new BSFManager();
manager.exec(“jruby”, filename, 0, 0, PathType.ABSOLUTE);

public static void main(String[] args) throws BSFException,
FileNotFoundException {
new BsfLoadPathSample();

BUT when I run this RB file,

require ‘fuzzystringmatch’
jarow = FuzzyStringMatch::JaroWinkler.create( :pure )
p jarow.getDistance( “jones”, “johnson” )

(by changing test.rb to fuzzy.rb )


LoadError: no such file to load – fuzzystringmatch
require at org/jruby/
(root) at c:/jruby/samples/fuzzy.rb:1
Oct 13, 2014 5:11:34 PM org.apache.bsf.BSFManager exec

HOW DO I get Java App to look into GEM files? Is there way to compile
GEMS into JAR file that I can include in my Java project? Any other

I am using JRuby 1.7.16


mkdir rubygems
GEM_PATH=rubygems GEM_HOME-rubygems jruby -S gem install mygem
cd rubygems
jar -cf …/gems.jar .
add this gems.jar along the jruby.jar on the same classpath/classloader
then jruby will pick your gems.

see also

for running inside OSGi or some j2ee containers there is a little more
do (the wiki would need some update here).

let me know if I was not clear enough . . . christian