JRuby on Rails: Adding a folder to the classpath
Asked Answered
H

1

17

I'm trying to add an entire folder to the JRuby 1.5 classpath for my Rails app. The JRuby Wiki suggests the following: "... add the config directory to the JRuby classpath in config/environment.rb:"

$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, 'config'))}/"

That doesn't seem to work for me. It doesn't matter whether I put that before, after or inside of the Rails::Initializer.run block. No matter what, I get:

/home/sean/src/sbruby/seo/config/environment.rb:45:NoMethodError: undefined method `<<' for nil:NilClass
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController

For example, I'm trying to add a folder under RAILS_ROOT called resources/foobar, so I added the following to environment.rb:

$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, "resources", "foobar"))}/"

Same error.

What is the right way to add a folder to the JRuby classpath with Rails?

Hiatus answered 6/7, 2010 at 23:13 Comment(0)
I
28

Require java first. That's what makes the $CLASSPATH variable live.

include Java
$CLASSPATH << "your/folder"

In pre-1.0 versions of JRuby, you'd do require 'java' instead, but in modern JRuby that silently doesn't work.

Immunotherapy answered 7/7, 2010 at 4:27 Comment(3)
Awesome. That was what I was missing!Hiatus
I updated the JRuby wiki as well to reflect that requirement. Thanks again.Hiatus
although it is "include Java" rather than "require 'java'" these days (blogs.oracle.com/coolstuff/entry/using_java_classes_in_jruby).Exasperate

© 2022 - 2024 — McMap. All rights reserved.