Archive for January, 2011

JRuby, Can’t Find File in Classpath (You Needed a Slash, in

Monday, January 10th, 2011

Update: this is no longer valid as of the release of 1.6.0.

I was debugging this Dubious problem where a class I was sure was on the class path couldn’t be found(link). First, I tried compiling on my local checkout, and it worked oddly. Then I realized I was dumb, because the class was being picked up from the finished jar. So, I clobbered my build artifacts and then my local copy didn’t compile either.

I did some digging and found out why.

In the end it was because of how JRuby implements $CLASSPATH–the way you can modify the classpath from within JRuby. JRuby uses a subclass of URLClassLoader to implement it which gives it some possibly unexpected behavior. In particular, URLClassLoader treats anything that does not end in a slash as a jar. That’s why the class files in my build directory were not being picked up properly.

Lessons Learned:

Combat “It Works on My Machine™”. Clobber local generated files every now and again, and checkout a fresh tree from time to time to reduce the chance something local is weird.

Look for and ask for help when you don’t know something. First time I tried to figure out the problem, I just tinkered and compiled and tinkered and compiled. A little research would have saved some time.

TL;DR

If JRuby can’t find your classes and you think they should have been added to $CLASSPATH, make sure you have trailing slashes on your directory names.

Update:

This has actually been fixed, but not released yet: https://github.com/jruby/jruby/commit/8740a6b3ea946a1442dd7fa833aed3c30d82e23f