weixin_39725154
weixin_39725154
2020-11-22 11:27

LoadError: load error: META-INF/main -- java.sql.SQLException: No suitable driver found for jdbc:postgresql

I am trying to use warble to create a JAR, which happens to use a postgresql driver jar to connect to Amazon RedShift.

When I create the jar and execute, I get the following exception:

 bash
$ java -jar test.jar
LoadError: load error: META-INF/main -- java.sql.SQLException: No suitable driver found for jdbc:postgresql://MYHOST.redshift.amazonaws.com:MYPORT/MYDB?user=MYUSER&password=MYPASSWORD
  require at org/jruby/RubyKernel.java:1054
  require at jar:file:/var/folders/2w/n_00rw7x0fx8tkx_tgk4kq7m0000gn/T/jruby8811939749765534549extract/jruby-stdlib-1.7.4.jar!/META-INF/jruby.home/lib/ruby/shared/rubygems/custom_require.rb:36
   (root) at <script>:3
error: org.jruby.embed.EvalFailedException: (LoadError) load error: META-INF/main -- java.sql.SQLException: No suitable driver found for jdbc:postgresql://MYHOST.redshift.amazonaws.com:MYPORT/MYDB?user=MYUSER&password=MYPASSWORD
I separated the connectivity code to demonstrate that it works prior to creating the Jar. Current files:
 bash
$ tree
.
├── Gemfile
├── Gemfile.lock
├── bin
│   └── main.rb -> ../main.rb
├── lib
│   └── postgresql-9.2-1003.jdbc4.jar
├── main.rb
└── test.jar

2 directories, 6 files
File contents: main.rb
 ruby
#!/usr/bin/env jruby

require 'rubygems'

$: << "./lib"

require 'postgresql-9.2-1003.jdbc4.jar'

puts $CLASSPATH

jdbc_url = "jdbc:postgresql://MYHOST.redshift.amazonaws.com:MYPORT/MYDB?user=MYUSER&password=MYPASSWORD"

java_import java.sql.DriverManager
DriverManager.register_driver(org.postgresql.Driver.new)
DriverManager.get_connection(jdbc_url)

puts "Done."

Execution output using JRuby (JIT compiled):
 bash
$ ./main.rb
["file:/opt/test/./lib/postgresql-9.2-1003.jdbc4.jar"]
Done.
Jar creation:
 bash
$ warble jar
No executable matching config.jar_name found, using bin/main.rb
rm -f test.jar
Creating test.jar
Jar execution:
 bash
$ java -jar test.jar
["jar:file:/opt/test/test.jar!/test/lib/postgresql-9.2-1003.jdbc4.jar"]
LoadError: load error: META-INF/main -- java.sql.SQLException: No suitable driver found for jdbc:postgresql://BLAH
Any thoughts on why this might not be working? Perhaps related to this? https://github.com/jruby/jruby/wiki/FAQs#why-do-i-get-classnotfoundexception-when-i-call-javalangclassforname-from-ruby Environment notes:
 bash
$ ruby -v
jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.6.0_51-b11-457-11M4509 [darwin-x86_64]

$ gem list

*** LOCAL GEMS ***

activemodel (4.0.0)
activerecord (4.0.0)
activerecord-deprecated_finders (1.0.3)
activerecord-jdbc-adapter (1.3.0, 1.2.9.1)
activerecord-jdbcmysql-adapter (1.3.0, 1.2.9)
activesupport (4.0.0)
ansi (1.4.3)
arel (4.0.0)
atomic (1.1.13 java, 1.1.10 java)
aws-sdk (1.16.1, 1.15.0, 1.14.1, 1.11.1)
bouncy-castle-java (1.5.0147)
builder (3.2.2, 3.1.4)
bundler (1.3.5)
bundler-unload (1.0.1)
hashr (0.0.22)
i18n (0.6.5, 0.6.4)
jdbc-mysql (5.1.25)
jruby-jars (1.7.4)
jruby-launcher (1.0.19 java, 1.0.18 java)
jruby-openssl (0.8.8)
jruby-rack (1.1.13.2)
json (1.8.0 java)
mail (2.5.4)
mime-types (1.25, 1.23)
mini_portile (0.5.1)
minitest (5.0.6, 4.7.5)
multi_json (1.7.9, 1.7.7)
nokogiri (1.6.0 java, 1.5.10 java)
polyglot (0.3.3)
rake (10.1.0)
rest-client (1.6.7)
rubygems-bundler (1.2.2)
rubyzip (1.0.0, 0.9.9)
rvm (1.11.3.8)
thread_safe (0.1.2 java)
tire (0.6.0)
treetop (1.4.15)
tzinfo (1.0.1, 0.3.37)
uuidtools (2.1.4)
warbler (1.3.8)

$ java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)

该提问来源于开源项目:jruby/warbler

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39725154 weixin_39725154 5月前

    I extracted the warble'd jar, copied the postgresql jars from ./test/lib to ./META-INF/lib/, and recompressed the Jar. Seems to have worked. More testing details to follow.

    点赞 评论 复制链接分享
  • weixin_39633113 weixin_39633113 5月前

    I had the exact same issue, but with the SQL Server jdbc driver. I found that if I modify config/warble.rb and include my jar in config.java_libs, then warbler copies (doesn't move) my jar from lib into META-INF/lib.

    
    config.java_libs += FileList["lib/sqljdbc4.jar"]
    

    Seeing as how this is a configuration option, this probably isn't a bug that needs to be fixed.

    点赞 评论 复制链接分享
  • weixin_39562752 weixin_39562752 5月前

    this is a common issue when using "raw" JDBC drivers and Warbler can not do anything about it ... we recommend using database: postgresql (instead of "jdbc") with AR/Rails or for "raw" usage the driver gems (if applicable) e.g. require 'jdbc/postgres'; Jdbc::Postgres.load_driver for commercial drivers one simply needs to get the driver jars on the class-path just like did.

    点赞 评论 复制链接分享
  • weixin_39725154 weixin_39725154 5月前

    Some articles I read say to execute... Class.forName("org.postgresql.Driver"); ...before executing: DriverManager.get_connection(jdbc_url)

    I assume the JRuby equivalent is: Java::JavaClass.for_name("org.postgresql.Driver")

    In (JRuby) IRB, it looks like it helps (see pre/post postgresql jar file include):

     ruby
    $ irb
    jruby-1.7.4 :001 > Java::JavaClass.for_name("org.postgresql.Driver")
    NameError: cannot load Java class org.postgresql.Driver
        from org/jruby/javasupport/JavaClass.java:1242:in `for_name'
        from (irb):1:in `evaluate'
        from org/jruby/RubyKernel.java:1093:in `eval'
        from org/jruby/RubyKernel.java:1489:in `loop'
        from org/jruby/RubyKernel.java:1254:in `catch'
        from org/jruby/RubyKernel.java:1254:in `catch'
        from /Users/Eric/.rvm/rubies/jruby-1.7.4/bin/irb:13:in `(root)'
    jruby-1.7.4 :002 > require './lib/postgresql-8.4-703.jdbc4.jar'
     => true
    jruby-1.7.4 :003 > Java::JavaClass.for_name("org.postgresql.Driver")
     => class org.postgresql.Driver
    

    But when I add that line to my main app, and create a Jar, the problem persists (same error).

    点赞 评论 复制链接分享
  • weixin_39725154 weixin_39725154 5月前

    I extracted the contents of the created jar file, and grep'd for Jars..

     bash
    $ jar xf test.jar
    
    $ find . | grep -i \.jar$
    ./bundler/gems/warbler-75fc2de2f4c1/lib/warbler_jar.jar
    ./bundler/gems/warbler-75fc2de2f4c1/spec/sample_jar
    ./bundler/gems/warbler-75fc2de2f4c1/spec/sample_jar/bin/another_jar
    ./bundler/gems/warbler-75fc2de2f4c1/spec/sample_jar/bin/sample_jar
    ./gems/activerecord-jdbc-adapter-1.3.0/lib/arjdbc/jdbc/adapter_java.jar
    ./gems/atomic-1.1.13-java/lib/atomic_reference.jar
    ./gems/jdbc-postgres-9.2.1002.1/lib/postgresql-9.2-1002.jdbc3.jar
    ./gems/jdbc-postgres-9.2.1002.1/lib/postgresql-9.2-1002.jdbc4.jar
    ./gems/jruby-jars-1.7.4/lib/jruby-core-1.7.4.jar
    ./gems/jruby-jars-1.7.4/lib/jruby-stdlib-1.7.4.jar
    ./gems/jruby-rack-1.1.13.2/lib/jruby-rack-1.1.13.2.jar
    ./gems/json-1.8.0-java/lib/json/ext/generator.jar
    ./gems/json-1.8.0-java/lib/json/ext/parser.jar
    ./gems/nokogiri-1.6.0-java/lib/isorelax.jar
    ./gems/nokogiri-1.6.0-java/lib/jing.jar
    ./gems/nokogiri-1.6.0-java/lib/nekodtd.jar
    ./gems/nokogiri-1.6.0-java/lib/nekohtml.jar
    ./gems/nokogiri-1.6.0-java/lib/nokogiri/nokogiri.jar
    ./gems/nokogiri-1.6.0-java/lib/xercesImpl.jar
    ./gems/thread_safe-0.1.2-java/lib/thread_safe/jruby_cache_backend.jar
    ./META-INF/lib/jruby-core-1.7.4.jar
    ./META-INF/lib/jruby-stdlib-1.7.4.jar
    ./test/lib/postgresql-8.4-703.jdbc4.jar
    ./test/lib/postgresql-9.2-1003.jdbc4.jar
    ./test.jar
    

    Does it look like the postgresql Jars are in the correct directory?

    点赞 评论 复制链接分享

相关推荐