JAR generated by warbler cannot access included internal JAR library

守給你的承諾、 提交于 2019-12-08 06:19:32

问题


I have a JRuby project that connects to an Oracle database using JDBC via Oracle's ojdbc6.jar library. The code works well when run using JRuby 1.6.6 on Windows 7 and JRuby 1.6.5.1 on OS X Lion. I'm trying to create a standalone JAR file using warbler. After running warble jar it includes the ojdbc6.jar but for some reason it does not load/access it. It seems the internal classpath is incorrect or I'm not configuring something right.

Following directory structure exists.

C:\my_jruby_project\bin\my_jruby_file.rb
C:\my_jruby_project\lib\java\ojdbc6.jar
C:\my_jruby_project\Gemfile

C:\my_jruby_project\Gemfile:

source :rubygems
gem 'activerecord', '>= 3.2.3'
gem 'activerecord-jdbc-adapter', '>= 1.2.2'
gem 'ruport', '>= 1.6.3'

C:\my_jruby_project\bin\my_jruby_file.rb

require 'ruport'
require 'java'

java_import 'oracle.jdbc.OracleDriver'
java_import 'java.sql.DriverManager'
....

After generating the JAR file:

jruby -S warble jar

I execute the jar and get the following error:

C:\my_jruby_project>java -jar my_jruby_project.jar
NameError: cannot load Java class oracle.jdbc.OracleDriver
         for_name at org/jruby/javasupport/JavaClass.java:1205
  get_proxy_class at org/jruby/javasupport/JavaUtilities.java:34
      java_import at file:/C:/Users/DAVIDH~1.OPE/AppData/Local/Temp/jruby8647327738550400677extract/
jruby-core-1.6.7.jar!/builtin/javasupport/core_ext/object.rb:46
           (root) at file:/C:/my_jruby_project/my_jruby_project.jar!/my_jruby_project/bin/my_jruby_file.rb:4
             load at org/jruby/RubyKernel.java:1058
           (root) at file:/C:/my_jruby_project/my_jruby_project.jar!/my_jruby_project/bin/my_jruby_file.rb:1
          require at org/jruby/RubyKernel.java:1033
          require at file:/C:/my_jruby_project/my_jruby_project.jar!/META-INF/main.rb:36
           (root) at <script>:3

C:\my_jruby_project>

The generated JAR includes the lib/java/ojdbc6.jar but it seems internal file or path pointers are not configure correctly.

Appreciate any assistance. Thanks!


回答1:


You may want to setup $CLASSPATH inside your ruby code. Something similar to this:

require 'java'
$CLASSPATH << "lib/java/ojdbc6.jar"

I use some extra jars in my jruby on rails project and have the following code in my environment.rb in order for the .jars to be accessible in my .war deployment:

require 'java'
Dir.glob("lib/*.jar").each do |jar|
  $CLASSPATH << "#{Rails.root.to_s}/#{jar}"
end


来源:https://stackoverflow.com/questions/11122130/jar-generated-by-warbler-cannot-access-included-internal-jar-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!