How to include class dependency jar in JRuby?

Viewed 551

I'm having trouble figuring out how to include/import/require mydependency.jar which MyJavaClass (within a MyJavaClass.class file) depends on. MyJavaClass contains many import statements importing classes from mydependency.jar. Everything is in the same directory.

When I run java -cp '.:mydependency.jar' MyJavaClass on the command line, I get no error and by putting some calls in main I can get it to run how I want, but when I run ruby my_ruby_file.rb with JRuby on the command line I get NoClassDefFoundError for the first class imported (which is being imported from mydependency.jar) within the MyJavaClass.class file. The stack trace for the error points to the java_import line in my_ruby_file.rb so it seems like it's not properly finding the classes imported from mydependency.jar that are included in my MyJavaClass.class file, even though it runs fine in the normal Java environment.

# contents of my_ruby_file.rb

require 'java'
require './mydependency.jar'

java_import 'MyJavaClass'

MyJavaClass.new.myJavaInstanceMethod('argument')
1 Answers

My $CLASSPATH environment variable was not set, which it needs to be in order for JRuby to be able to find my jar. After I ran export CLASSPATH=".:mydependency.jar" on the command line and removed the line require './mydependency.jar', it worked.

Related