Saturday, January 3, 2009

JRuby & Java Communication

I recently spent several frustrating days trying to get some JRuby code to call some Java classes. The problem seemed to be that the individual Java classes required some jars that weren't on the JRuby classpath, although they were on the classpath for the Java project they belonged to. I still haven't figured out the proper way to fix that, and the only solution I've found so far is to include the following line in my JRuby code:

"$CLASSPATH << path/to/something.jar"

This includes the required jar in the JRuby classpath. I'd really like to be able to just give a -cp (or similar) command to the JRuby interpreter, but haven't been able to figure out what that would be yet.

The next issue was that there were some dlls required by the Java class. I'm still not sure why, since when the Java project is run independently it isn't necessary to tell the JVM where the dlls are, but when the classes are called individually the dlls aren't found, leading to problems. The only way I could find to fix this problem is to include the following line in my Java class:

"System.load(path/to/myDLL.dll)"

Now the entire thing is up and running, but I wish there were a better way to solve these problems. It took me a while to even get this far, so hopefully someone else having the same problem will find this helpful.

2 comments:

Nick Sieger said...

If you set the CLASSPATH environment variable before launching JRuby, JRuby will honor it, adding its own classes/jars to those that are already in the classpath. That's probably the easiest way to avoid hard-coding paths in your script.

You can also use the "-J" argument prefix to pass arguments to the JVM.

Kate said...

I must be doing something wrong - I tried setting the CLASSPATH environment variable but it didn't work.

I tryed using -J and that didn't work either. -J-cp=""? It didn't recognize it. Maybe I have the part that follows -J wrong?

Thanks so much for your help!