English 中文(简体)
How to register a JDBC driver using jruby-complete.jar?
原标题:
  • 时间:2009-11-19 16:42:51
  •  标签:
  • jruby

I m trying to write a script that is executed with the jruby-complete.jar like so:

java -cp derby.jar; -Djdbc.drivers=org.apache.derby.jdbc.EmbeddedDriver -jar jruby-complete.jar -S my_script.rb

I m using JVM 1.6.0_11 and JRuby 1.4.

In my jruby script I attempt to connect to the database like this.

connection = Java::com.sql.DriverManager.getConnection("jdbc:derby:path_to_my_DB")

This throws a java.sql.SQLException: "No suitable driver found" exception.

I ve tried manually loading the driver into the class loader using Class.forName which gives me the same error.

It looks like to me that the class loader being used by the DriverManager is not the same as the current thread s. I ve tried setting the current thread s class loader using:

JThread = java.lang.Thread 
...
class_loader = JavaLang::URLClassLoader.new(  
  [JavaLang::URL.new("jar:file:/derby.jar!/")].to_java(
    JavaLang::URL),JRuby.runtime.jruby_class_loader)      

JThread.currentThread().setContextClassLoader(class_loader )    

But this doesn t help.

Any ideas?

最佳回答

OK I downloaded jruby-complete.jar and had a go....

This seems to work for me:

java -classpath c:
ubydb-derby-10.5.3.0-binlibderby.jar;jruby-complete-1.4.0.jar org.jruby.Main -S derby.rb

When using the -jar switch, the -classpath option is ignored (maybe the CLASSPATH shell var is too). But on the above line, we put both required jars on the class path and pass the class name to execute (i.e. org.jruby.Main). The script being passed in is as per my other answer.

Another option (which I have not tried) would be to alter the jruby-complete.jar manifest file to specify as classpath, as described here: Adding Classes to the JAR File s Classpath

问题回答

First, make sure your driver jar is not corrupted (this made me waste a couple of days one time).

Second, read this about JRuby/Java classloade: JRuby Wiki

Third (because I haven t played with "jruby-complete") try this simple script and then see if you can adapt as you need.

require  java 
require  C:
ubydb-derby-10.5.3.0-binlibderby.jar   # adjust for your machine
include_class "java.sql.DriverManager"
derby = org.apache.derby.jdbc.EmbeddedDriver.new
connection = DriverManager.getConnection("jdbc:derby:derbyDB;create=true")




相关问题
what do these ruby warnings mean?

cqq.rb:96 **warning: Statement not reached.** /root/newpackage/lib.rb:727 **warning: instance variable @object not initialized** Error: Your application used more memory than the safety cap of 500m. ...

How to register a JDBC driver using jruby-complete.jar?

I m trying to write a script that is executed with the jruby-complete.jar like so: java -cp derby.jar; -Djdbc.drivers=org.apache.derby.jdbc.EmbeddedDriver -jar jruby-complete.jar -S my_script.rb I m ...

How do I unmarshal a ruby object in java?

I have a an object that I d like to grab the contents of in java. The only problem is that is is currently in ruby. irb(main):050:0> blah => "...

What s the difference between Ruby and JRuby? [closed]

Can anyone please provide me in layman s terms the difference between developing a JRuby and a Ruby, Rails application? I use NetBeans as my Ruby on Rails IDE and every-time I create a project is ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

热门标签