How does one debug a jruby project that has an embedded jar?

So what I would like to do is debug a jruby project that contains a Jar. But no matter what I do I cannot seem to be able to do this. Despite all attempts, the jar does not seem to be loaded, or the classloader is not finding the classes because it is associated to a different classloader? Unsure.

But anyways, this is bone stock sort of stuff, here is a promintent Github project that illustrates the issue:

https://github.com/jruby/activerecord-jdbc-adapter

Namely, open the activerecord-jdbc-adapter-master/jdbc-mysql directory as a project, add the lib directory to the project path, add a simple test such as:

require 'rubygems'
require 'jdbc/mysql'
require 'java'

puts "enumerating..."
java.sql.DriverManager.getDrivers.each{ |e| puts e }

url = "jdbc:mysql://localhost/test"
connection = java.sql.DriverManager.getConnection url

You will frequently run into one of two classes of errors:

1. no such file to load -- jdbc/mysql
2. DriverManager.java:602:in `getConnection': java.sql.SQLException: No suitable driver found for jdbc:...

If someone could provide a specific example of debugging a ruby gem that has a JAR file located in the lib directory, a tested working example, I'd appreciate it. At this point I have not found any way to make either RubyMine or IntelliJ+Ruby-Plugin work.

2 comments

Maybe I am not clear about what you ask. Here is the way I setup my classpath, I put a initializer there containing the following code

jars_folder = Dir.pwd + "/lib/jars"

Dir.entries(jars_folder).each do |jar_file|
  if RUBY_PLATFORM =~ /java/
    $CLASSPATH << (jars_folder + File::SEPARATOR + jar_file) if (/\.jar$/ =~ jar_file.to_s)
  end
end




and I put all my jar files under 'lib/jars'. Of course, I put my jdbc driver under the lib/jars folder too.

Lei
0

Thanks, let me try that Lei.

What I find strange is that the pattern used in the mentioned project does not work. Even by itself, without a debugger, none of the activerecord-jdbc code works at all for me, because of this:

    def self.driver_jar
      "mysql-connector-java-#{DRIVER_VERSION}.jar"
    end

    def self.load_driver(method = :load)
      send method, driver_jar
    end

    def self.driver_name
      'com.mysql.jdbc.Driver'
    end

    if defined?(JRUBY_VERSION) && # enable backwards-compat behavior :
      ( Java::JavaLang::Boolean.get_boolean("jdbc.driver.autoload") ||
        Java::JavaLang::Boolean.get_boolean("jdbc.mysql.autoload") )
      warn "autoloading JDBC driver on require 'jdbc/mysql'" if $VERBOSE
      load_driver :require
    end


The code fails. Let me try our own driver the way you suggest.

0

Please sign in to leave a comment.