Tuesday, October 13, 2009

Embedding ruby in java - less than trivial so far

Currently I am trying to get puppet, a configuration management system to be executed in Jruby - and especially embedded within some java code.

Luckily there is the JSR 223 scripting available in Java6, so the start is relatively easy: get jruby from jruby.org and add it to the java programs classpath, java program code looks like this:


// Define path to jruby libraries
System.setProperty("jruby.home","/opt/local/share/java/jruby");

// Get the scripting engine
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName("jruby");

// Define the puppet script to run as arguments
rubyEngine.put(ScriptEngine.ARGV,new String[]{"example.pp"});

// read the script
File f = new File("puppet.rb");
BufferedReader br = new BufferedReader(new FileReader(f));

// and execute it
rubyEngine.eval(br, context);


Simple - eh?

Unfortunately is it not that simple (yet).

Passing the argument vector to my script does not yet work in jruby 1.4 cr1 (see JRUBY-4090), so I need to do a work around of defining a script, that sets this and then calls puppet


ARGV << 'example.pp'

require 'puppet/application/puppet'

Puppet::Application[:puppet].run



Next, puppet is complaining about missing stuff like OpenSSL (which is a difficult beast in jruby, as the standard ruby implementation needs some C code, which can not directly run in jruby).

Also it is somehow complaining about yecht not being found - I still don't really grok setting and properties up things like jruby.lib, jruby.home etc.

If anyone can shed some light on it, I would appreciate it :-)

1 comment:

  1. You might try using jruby-complete.jar for embedding rather than hard-coding jruby.home -- then you wouldn't need that step at all.

    As far as OpenSSL, you'll need the jruby-openssl gem; but in order to use it w/ embedded JRuby you'll need to put the contained jopenssl.jar on the classpath and package the gem in a jar as described here.

    ReplyDelete