Question regarding the combination of embedded JRuby and gems-in-a-jar

Hello,

I’m currently creating a plugin for dotCMS, a Java CMS, such that it is
possible to use Sass/Compass stylesheets, and as such I’m trying to use
JRuby to do this (this will require one or more Sass/Compass plugins
that
will need to access the Java API of dotCMS) .
For now this needs to work on Windows, of course, in the end it should
work
wherever the JVM runs.

I’ve managed to get the gems (by installing compass) jarred up using the
blog post by Nick S. [1] (only change is that I just zipped the
output
directory, couldn’t immediately get the command to run as is).
And when I test this on the command line I can get it to work (from any
directory, the gems in a jar is in the C:/dotCMS_SASS directory,
jruby-complete.jar was in another directory) using:
java -jar jruby-complete-1.6.5.1.jar -IC:/dotCMS_SASS -rcompass-gems.jar
-S
irb

In the repl I can then require rubygems and then compass without
problems.
However when I try the same embedded, it can’t even find rubygems, never
mind compass.

I’m initializing a RubyConfigInstance with the following parameters:
RubyInstanceConfig config = new RubyInstanceConfig();
config.processArguments(new String[] {“-I” + libPath.replace(‘\’, ‘/’),
“-rcompass-gems.jar”});
Which would seem to be enough considering I could start the repl above
with
the same parameters.
Here libPath is the absolute directory (programmatically obtained) where
compass-gems.jar and jruby-complete-1.6.5.1.jar are located (see also a
bit
below for more details).

The Ruby runtime instance is then created using:
Ruby ruby= JavaEmbedUtils.initialize(Arrays.asList(), config);
Using that I can do the simple stuff such as:
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
evaler.eval(ruby, “puts "Hello from the jRuby World!"”);

However when I do this:
evaler.eval(ruby, “require ‘rubygems’”);
then I get:
org.jruby.exceptions.RaiseException: (LoadError) no such file to load –
rubygems
at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java:1038)
at #Class:0x37de84.(root)(:1)

And I can’t require compass either.

Can you please advise what I need parameters I need to add to do to get
the
gems in a jar to load in an embedded situation?

I’m using JDK1.6_u26, 32 bit, on Windows XP, and I’m currently using the
jruby-complete-1.6.5.1.jar and running atom dotCMS 1.9.4.1.
I’ve before tried the ‘plain’ jruby.jar before, however since I got the
missing rubygems I’ve tried to use jruby-complete to see if that would
work
better.

It would seem, though I can’t verify, that the jruby jar is loaded from
inside another jar by the dotCMS classloader (which is a Tomcat6 root
deployment), however I’m not sure this should matter since I can put the
jruby jar on any place and run irb and get compass (and rubygems) to
load.

Thank you for your time,
Maarten Daalder
@MLeoDaalder

[1]
http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar

Since writing my question I’ve found out that dotCMS contains it’s own
(old)
version of jruby.jar [1].

However, I have also figured out how to evade it, through extending an
URLClassLoader.

It turns out to be a fairly generalized way of getting a ‘copy’ of the
class
hierarchy (of course just from the jars that you include) based from
just
the system classloader (the JRE). This turned out to be what we needed.
I’ve also added a way to inject some classes (and their
superclasses/interfaces) from outside the jars that you include. This
way
you can use ‘bridge’ classes/interfaces so that you won’t need to use a
lot
of reflection (except to create a new instance of course).

I hope this will help future developers trying to use JRuby in a system
that
already uses some other, older, locked down version.

–Maarten Daalder
@MLeoDaalder

[1] jruby 1.3.0 (ruby 1.8.6p287) (2009-06-03 5dc2e22) (Java HotSpot™
Client VM 1.7.0_01) [x86-java]

On 11 January 2012 16:59, MLeo [email protected] wrote:

I’ve managed to get the gems (by installing compass) jarred up using the
mind compass.

rubygems
I’ve before tried the ‘plain’ jruby.jar before, however since I got the
@MLeoDaalder

[1]
JRuby 1.1.6: Gems-in-a-jar


View this message in context:
http://ruby.11.n6.nabble.com/Question-regarding-the-combination-of-embedded-JRuby-and-gems-in-a-jar-tp3595190p3667317.html
Sent from the JRuby - User mailing list archive at Nabble.com.