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:

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. `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.


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)

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


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

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

    def self.driver_name

    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

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


Please sign in to leave a comment.