JAR generated by warbler cannot access included in

2019-06-01 10:58发布

问题:

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