如何正确添加到 JRuby on Rails 项目中的 CLASSPATH?
我通过在 config/application.rb 中添加一些外部 jar 到我的 CLASSPATH 中:
require 'java'
$CLASSPATH << "#{File.dirname(__FILE__)}/../backend/src/"
Dir["#{File.dirname(__FILE__)}/../backend/lib/*.jar"].each do |jar|
require jar
end
这在使用 Rails 服务器运行 Rails 时效果很好。加载 CLASSPATH 中的 jar。但是,运行 rake 时,未设置类路径。我该如何设置呢?特别是,我需要在加载 gem 之前设置它,因为我使用 JRClj gem,它依赖于我的 CLASSPATH 中已有的 clojure.jar。
(哦,我也尝试将代码放入environment.rb;没有帮助)
“不起作用”,我的意思是:
$ rake rspec --trace
rake aborted!
No such file to load -- /MY_CURRENT_WORKING_DIRECTORY/clojure.lang.RT
org/jruby/RubyKernel.java:1063:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:639:in `new_constants_in'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/default_loader.rb:6:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:569:in `load_imports'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:508:in `raw_load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `run'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/bin/rake:33:in `(root)'
org/jruby/RubyKernel.java:1063:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/bin/rake:19:in `(root)'
I've added some external jars to my CLASSPATH by adding this in config/application.rb
:
require 'java'
$CLASSPATH << "#{File.dirname(__FILE__)}/../backend/src/"
Dir["#{File.dirname(__FILE__)}/../backend/lib/*.jar"].each do |jar|
require jar
end
This works fine when running Rails using rails server
. The jars from the CLASSPATH are loaded. However, when running rake
the classpath is not set. How can I set it? In particular, I need to set it before loading my gems, since I use the JRClj gem, which relies on already having clojure.jar in my CLASSPATH.
(Oh, I also tried putting the code in environment.rb; didn't help)
By "not work", I mean:
$ rake rspec --trace
rake aborted!
No such file to load -- /MY_CURRENT_WORKING_DIRECTORY/clojure.lang.RT
org/jruby/RubyKernel.java:1063:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:640:in `new_constants_in'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:639:in `new_constants_in'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:223:in `load_dependency'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/default_loader.rb:6:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:569:in `load_imports'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:508:in `raw_load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `run'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/gems/rake-0.9.2.2/bin/rake:33:in `(root)'
org/jruby/RubyKernel.java:1063:in `load'
/Users/pbiggar/.rvm/gems/jruby-1.6.5@jruby-clojure-combination/bin/rake:19:in `(root)'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为我使用的 gem 使用
import
而不是java_import
,点击 JRUBY 错误 3797。更多详细信息,请参见: JRuby:导入、包含、java_import 与include_class。问题中的代码是正确的。
This happened because a gem I was using used
import
instead ofjava_import
, hitting JRUBY bug 3797. More detail in: JRuby: import vs include vs java_import vs include_class.The code in the question is correct.