0

I am getting a 'No suitable driver found' error when running my jar generated with warbler. However when I run it as ruby the code succeeds. The command to load the driver returns true, leading me to believe it can still load the driver while in the jar. However I am unable to figure out why DriverManager.get_connection is erroring with No driver found. Especially since when running from Ruby it succeeds.

Ruby function being called

def self.connect(opts)
    connection = nil
    begin
      driver = Jdbc::PostgreSQL.load_driver

      connection = DriverManager.get_connection("jdbc:postgresql://host:port/postgres", opts[:username], opts[:password])
      connection.auto_commit = false
    rescue
       puts $!, $@
       ... 
    end
    connection
  end

Running Ruby

Test Connection Succeeded

Running JAR

No suitable driver found for jdbc:postgresql://host:port/postgres java.sql.DriverManager.getConnection(DriverManager.java:602) java.sql.DriverManager.getConnection(DriverManager.java:185)

warbler.rb

Gems to include

config.gems += ["trollop", "builder", "jdbc-postgres"]

Ruby imports

require, java_import

# All support libraries required to be included
[
  'java',
  'ostruct',
  'trollop',
  'logger',
  'fileutils',
  'yaml',
  'jdbc/postgres'
].each do |require_name|
  require require_name
end


 # All java imported namespaces
    [
        'java.sql.DriverManager'
    ].each do |namespace|
      java_import namespace
    end

It seems like something is not making it into the JAR which is causing the failure. Any suggestions would be greatly appreciated.

2 Answers 2

1

I got this working by removing the jdbc-postgres Gem, then specifically including the PostgreSQL JDBC JAR. I extracted the relative parts and left out the general error handling and functions.

require postgresql-9.3-1101.jdbc4.jar

Java::JavaClass.for_name "org.postgresql.Driver"
Java::JavaClass.for_name "java.util.Properties"

props = java.util.Properties.new
props.set_property :user, opts[:username]
props.set_property :password, opts[:password]

connection = org.postgresql.Driver.new.connect(get_jdbcurl(opts), props)
Sign up to request clarification or add additional context in comments.

Comments

0

likely PostreSQL's driver class does not get initialized (it's on the CP but no one used it so far) ... thus doing something of a Java::JavaClass.for_name Jdbc::PostgreSQL.driver_name (before DriverManager.get_connection) will register it with the DriverManager

1 Comment

Thanks, I added the initialize call, with no change and I ended up trying a bunch of calls to ensure the driver was being loaded. Everything ended up with the same exception when in the JAR. I ended up changing how I was using org.postgresql.Driver class to get it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.