Forum: JRuby Class.forName class not found exception

Posted by Anbu Sivakani (anbusivakani)
on 2010-03-03 10:38
require 'java'
module JavaLang
  include_package 'java.lang'
end

module JavaSql
  include_package 'java.sql'
end

JavaLang::Class.forName("com.mysql.jdbc.Driver").newInstance
...
.
.
.
...



This is the code that i tried in jruby(Netbeans IDE).

But i am getting class not found exception.(Class.forName). Can anybody
help me for this..
Posted by Charles Nutter (headius)
on 2010-03-03 14:47
(Received via mailing list)
Hah, looks like you found the example I posted on your own :) Answers 
below...

On Wed, Mar 3, 2010 at 3:38 AM, Anbu Sivakani <lists@ruby-forum.com> 
wrote:
> require 'java'
> module JavaLang
>  include_package 'java.lang'
> end

remove this; it's not needed

> module JavaSql
>  include_package 'java.sql'
> end
>
> JavaLang::Class.forName("com.mysql.jdbc.Driver").newInstance

replace with

com.mysql.jdbc.Driver.new

> This is the code that i tried in jruby(Netbeans IDE).
>
> But i am getting class not found exception.(Class.forName). Can anybody
> help me for this..

You probably don't have the MySQL jar anywhere in classpath. You can
get it either by downloading (google for "mysql jdbc driver") or by
installing the jdbc-mysql gem:

~/projects/jruby âž” gem install jdbc-mysql
Successfully installed jdbc-mysql-5.0.4
1 gem installed
Installing ri documentation for jdbc-mysql-5.0.4...
Installing RDoc documentation for jdbc-mysql-5.0.4...

~/projects/jruby âž” jruby -rjava -rubygems -e "require 'jdbc/mysql';
require 'java'; puts com.mysql.jdbc.Driver.new"
com.mysql.jdbc.Driver@10cec16

~/projects/jruby âž”
CLASSPATH=lib/ruby/gems/1.8/gems/jdbc-mysql-5.0.4/lib/mysql-connector-java-5.0.4-bin.jar
jruby -rjava -e "puts com.mysql.jdbc.Driver.new"
com.mysql.jdbc.Driver@c9b196

The last command line is basically how you'd use the downloaded MySQL
JDBC jar, too.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.