FAQs

From JRubyWiki

Jump to: navigation, search

Contents

General

Where can I find more information about the Ruby language?

The Ruby Language Home Page has links to a wide range of resources for learning about Ruby, downloading releases of the C implementation of Ruby, documentation on Ruby, and community outlets for talking about Ruby.

The Pragmatic Programmers are the publishers of the de facto standard Ruby text, "Programming Ruby". It is considered a must-have manual to the Ruby language and libraries.

Almost everything you learn about the Ruby language is directly applicable to working with JRuby. JRuby aims to be a drop-in replacement for the C implementation of Ruby.

How do I run JRuby without the command-line scripts?

Frequently users want to run JRuby via Eclipse, launched from an existing Java application, or other ways that do not use the command-line scripts. In addition to having jruby.jar and its dependency jars available in CLASSPATH, you must also have the following system properties set:

jruby.home=<path to root of JRuby installation>

You may also want to ensure the maximum memory is set higher than the JVM default; JRuby's own command-line scripts use a maximum of 256MB.

What if I want to use JRuby alongside C Ruby? How do I keep from getting confused?

You have two options: 1. Always invoke JRuby with e.g., jruby -S gem. 2. Put a handy bash snippet like this in your .bashrc to create "j" aliases to all the available commands (rails becomes jrails, rake becomes jrake, etc.)

   for f in $JRUBY_HOME/bin/*; do
     f=$(basename $f)
     case $f in jruby*|jirb*|*.bat|*.rb|_*) continue ;; esac
     eval "alias j$f='jruby -S $f'"
   done

Why can't JRuby find my installed gems?

JRuby can only see the gems installed with the gem command shipped with JRuby. The gems installed this way will be stored under $JRUBY_HOME/lib/ruby. If you try to run Rails and get the following error "Cannot find gem for Rails ~>1.2.2.0:" (or whatever version you are using) the problem is probably that you haven't installed the Rails-gem and it's dependencies with the JRuby gem-command.

When I implement a Java interface and provide my own initialize method, I can't construct it anymore.

In JRuby 0.9.0, any class implementing a Java interface must explicitly call "super" in their initializers to set up the interface proxy. Add a super call to your implementation's initialize method and it should function.

How do I call JRuby from my existing Java program?

If you do not intend to launch JRuby as a separate process, we recommend you use the Bean Scripting Framework (BSF) or Java 6's Scripting Support (JSR223). We do not recommend calling directly into the JRuby runtime, since that code is subject to change before 1.0.

Where can I find the javadoc?

Why do I get a NoSuchMethodError when running JRuby 0.9.0?

Some users will see the following error when running JRuby 0.9.0:

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.append(Ljava/lang/CharSequence;)Ljava/lang/StringBuffer;

      at org.jruby.RubyString.cat(RubyString.java:223)
      at org.jruby.RubyString.append(RubyString.java:508)
      at org.jruby.RubyString.concat(RubyString.java:518) 

This error comes about when JRuby is built under Java 5 or higher and run on Java 1.4. Java 5 has the append(CharSequence) method, so it uses that during compilation. Java 1.4 does not have that method.

The first release of JRuby 0.9.0 was mistakenly build with Java 5, so we released a second version compiled with Java 1.4. Make sure you're using a JRuby compiled with Java 1.4 if that's the version of Java you intend to run it under.

How do I get a Ruby backtrace when calling JRuby code from Java?

You need to do something along the lines of:

  try {
    rubyCode.doSomething();
  } catch (RaiseException e) {
    e.getException().printBacktrace(System.err);
  }

Note that Java 6's scripting via the BSF libraries may not preserve stack traces (and also launches much slower). It is often preferable (as of February 2007) to use JRuby's own integration.

Why do I get a java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.visit() error when running jruby 1.X with a custom classpath?

The problem stems from a ASM jar conflict. It was found by setting a custom classpath, which includes a version of Spring that has a conflicting ASM jar, and then invoking jruby. The easiest workaround is to build jruby-complete as a single jar and edit the jruby config file (not sure about windows, but it should be similar) to source only the jruby-complete jar. You will need to add ALL other jars, which i believe includes your jdbc driver as well.

 svn co http://svn.codehaus.org/jruby/tags/jruby-1_0/
 cd jruby-1_0
 ant jar-complete
 vi bin/jruby (see diff for edit)
   #diff bin/jruby bin/jruby.orig 
   #89c89
   #<     for j in "$JRUBY_HOME"/lib/jruby-complete.jar; do
   #---
   #>     for j in "$JRUBY_HOME"/lib/*.jar; do

How come Java can't find resources in class folders that I've appended to the $CLASSPATH global variable at runtime?

JRubyClassLoader extends java.net.URLClassLoader which treats any URL that doesn't end with a slash as a jar. See URLClassLoader[1]

Why don't my special characters (like ção) appear correctly in my swing app ?

The file encoding for the .rb file containing the special characters must be set to UTF-8

Running Rails

Why does script/server (WEBrick) terminate right after saying "Booting WEBrick..."?

If you are requiring ActiveRecord-JDBC in your environment and have not installed the gem, this will happen. Make sure you have the ActiveRecord-JDBC gem installed.

How do I avoid installing the Rails gem twice for both CRuby and JRuby?

You just need to set GEM_HOME to point to your CRuby's gem directory. For example, in tcsh:

setenv GEM_HOME /usr/local/lib/ruby/gems/1.8

Note that on Windows, you should specify the path using unix-style directory separators. For example, if ruby is installed in c:\ruby:

set GEM_HOME=c:/ruby/lib/ruby/gems/1.8

I get the error 'undefined method `cattr_accessor' for ActiveRecord::Base:Class (NoMethodError)' after configuring activerecord-jdbc. What is wrong?

You are not requiring activerecord-jdbc properly. Try requiring activerecord-jdbc in config/environment.rb this way:

require 'jdbc_adapter'
Personal tools