4

I'm using JRuby 1.7.2, along with DataMapper, and I'm getting an error I can't find an answer to.

I'm just testing out DataMapper along with MySQL 5.5 to see if it will run fine when I build around it. Here's the file I'm testing:

require "data_mapper"
require "keys"

DataMapper.setup(:default, "mysql://#{$user}:#{$pass}@localhost/test_db")

And when I run this, I get the error:

NameError: cannot load Java class com.mysql.jdbc.Driver

And it points to the DataMapper.setup line.

My Gemfile should be alright:

source :rubygems

gem "sinatra"
gem "trinidad"
gem "data_mapper"

# do a `sudo apt-get install libmysqlclient-dev` first
gem "dm-mysql-adapter"
gem "jdbc-mysql"

Is there anything I'm missing? I have MySQL set up with a user/pass locally already.

4 Answers 4

7

This is a common error when running JDBC clients for MySQL. You need to make sure you have mysql-connector-java-bin.jar on the classpath. You can download it from here.

Sign up to request clarification or add additional context in comments.

Comments

7

I'm using Rails 3.2.9 and to solve the problem I added this to my application.rb

if defined? JRUBY_VERSION
    require 'jdbc/mysql'
    Jdbc::MySQL.load_driver
end

Comments

2

actually, the correct answer is to work-around an incompatibility between 5.1.13 and 5.1.22 !

jdbc-mysql gem already contains the mysql-connector.jar it just ain't auto-loading anymore :

https://github.com/jruby/activerecord-jdbc-adapter/tree/master/jdbc-mysql (read the README)

you can also force the driver to auto-load using a Java system property e.g. from a cmd line :

jruby -J-Darjdbc.mysql.autoload=true -S rake ...

Comments

0

I had the same error when trying to use a mysql database from jruby/rails4. In my case this line was missing from ./Gemfile:

gem 'jdbc-mysql'

Comments

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.