如何正确添加到 JRuby on Rails 项目中的 CLASSPATH?

发布于 2024-12-15 02:49:04 字数 3027 浏览 0 评论 0原文

我通过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-12-22 02:49:04

发生这种情况是因为我使用的 gem 使用 import 而不是 java_import,点击 JRUBY 错误 3797。更多详细信息,请参见: JRuby:导入、包含、java_import 与include_class

问题中的代码是正确的。

This happened because a gem I was using used import instead of java_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文