Using Gems With JRuby
By divas on Feb 18, 2009
If you are new to JRuby, you might not know that you cannot use a gem that has native extensions with JRuby, unless that gem builds its library in a platform-neutral manner. When you use the JRuby gem executable to try to add such a gem to your repository, you will get a message like this.
Building native extensions. This could take a while... ERROR: Error installing rmagick: ERROR: Failed to build gem native extension. C:/jruby/jruby-1.1.5/bin/jruby.bat extconf.rb install rmagick --no-rdoc --no-ri C:/jruby/jruby-1.1.5/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError) from C:/jruby/jruby-1.1.5/lib/ruby/1.8/mkmf.rb:1:in `require' from extconf.rb:1 Gem files will remain installed in C:/rubyrepos/jrubygemrepo/gems/rmagick-2.9.1 for inspection. Results logged to C:/rubyrepos/jrubygemrepo/gems/rmagick-2.9.1/ext/RMagick/gem_make.out
As you would guess, you then need to hunt for an equivalent gem that will work with JRuby. Anything with "pure Ruby" in its description is a good bet. In the case of RMagick, there is a RMagick4j. Some other examples JRuby compatible gems are Mongrel, Hpricot, ImageVoodoo, JRuby-OpenSSL, JRuby/LDap, json_pure, and postgres_pr.
If you are developing with both Ruby and JRuby, you might want to create a separate repository that holds the gems that work for both implementations, and then put that repository into your GEM_PATH. That way, you won't have to install the gem twice. If you are using NetBeans IDE, use the Ruby Platform Manager to add the repository to a platform's GEM_PATH.
If you want to require a certain gem based on whether you are running on JRuby, you can check whether JRUBY_VERSION is defined.