Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

jdbc

jruby

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.

like image 810
Imnotanerd Avatar asked Dec 05 '22 13:12

Imnotanerd


2 Answers

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.

like image 165
Reimeus Avatar answered Dec 07 '22 01:12

Reimeus


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
like image 21
Darren Hicks Avatar answered Dec 07 '22 01:12

Darren Hicks