Forum: JRuby Jars in Gems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul F. (Guest)
on 2009-05-15 01:50
(Received via mailing list)
Using netbeans, a required jar is referenced from the java configuration
and all works well.
Creating a gem containing the jar, how is the jar found when using the
gem.
I have tried "require", "include_package", "include_class" but none seem
to work for me.
How should this be set up?

Thanks
Paul F Fraser

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

    http://xircles.codehaus.org/manage_email
Nick S. (Guest)
on 2009-05-15 03:57
(Received via mailing list)
On Thu, May 14, 2009 at 4:49 PM, Paul F. <removed_email_address@domain.invalid>
wrote:
> Using netbeans, a required jar is referenced from the java configuration and
> all works well.
> Creating a gem containing the jar, how is the jar found when using the gem.
> I have tried "require", "include_package", "include_class" but none seem to
> work for me.
> How should this be set up?

The jar must be on the $LOAD_PATH. Usually when a gem gets loaded,
directories like $YOUR_GEM/lib will be added to the load path. So if
you place your jar file in $YOUR_GEM/lib/your_gem_ext.jar, you can
simply load the jar file by doing 'require "your_gem_ext"'.

Hope that answers the question,
/Nick

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

    http://xircles.codehaus.org/manage_email
Paul F. (Guest)
on 2009-05-15 17:27
(Received via mailing list)
Nick S. wrote:
> The jar must be on the $LOAD_PATH. Usually when a gem gets loaded,
> directories like $YOUR_GEM/lib will be added to the load path. So if
> you place your jar file in $YOUR_GEM/lib/your_gem_ext.jar, you can
> simply load the jar file by doing 'require "your_gem_ext"'.
>
> Hope that answers the question,
> /Nick
Hi Nick,
Checking the project before creating the gem.. I am using "Netbeans Dev
200905120201"

(1) require 'h2-1.1.106'  (the h2 database jar)  does NOT work with
h2-1.1.106.jar in the project/lib dir
but works with
(2) C:\Users\paul\dev\netbeans_projects\db_all\lib\h2-1.1.106.jar
added  in netbeans  Project Properties > Java > JRuby Classpath (tab)

the    c:\Users\paul\dev\netbeans_projects\db_all\lib  is being added to
the LOAD_PATH  ok.

I think (1)  is how you suggest it should work.  Any ideas..

Paul

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

    http://xircles.codehaus.org/manage_email
Colin Jones (Guest)
on 2009-05-15 17:57
(Received via mailing list)
We found we needed to do something like this:

require 'sinatra.jar'
gem :sinatra

before using it in our application.

---
Colin Jones

On May 15, 2009, at 8:26 AM, Paul F. <removed_email_address@domain.invalid> 
wrote:

>>> none seem to
>> /Nick
>
>   http://xircles.codehaus.org/manage_email
>
>

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

    http://xircles.codehaus.org/manage_email
Paul F. (Guest)
on 2009-05-15 18:34
(Received via mailing list)
Colin,
Thanks for your input.
Unfortunately --
After changing the jar name to h2 (gem :h2-1.1.106 won't work) and
trying
require 'h2.jar'
gem : h2
still same problem.

Paul


Colin Jones wrote:
> On May 15, 2009, at 8:26 AM, Paul F. <removed_email_address@domain.invalid> wrote:
>>>> I have tried "require", "include_package", "include_class" but none
>>> Hope that answers the question,
>>
>> the    c:\Users\paul\dev\netbeans_projects\db_all\lib  is being added
>> to the LOAD_PATH  ok.
>>
>> I think (1)  is how you suggest it should work.  Any ideas..
>>
>> Paul
>>


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

    http://xircles.codehaus.org/manage_email
Nick S. (Guest)
on 2009-05-15 18:51
(Received via mailing list)
On Fri, May 15, 2009 at 8:26 AM, Paul F. <removed_email_address@domain.invalid>
wrote:
>>> I have tried "require", "include_package", "include_class" but none seem
>> Hope that answers the question,
>  in netbeans  Project Properties > Java > JRuby Classpath (tab)
>
> the    c:\Users\paul\dev\netbeans_projects\db_all\lib  is being added to the
> LOAD_PATH  ok.
>
> I think (1)  is how you suggest it should work.  Any ideas..

So "db_all" is the name of the project? The "lib" directory doesn't
normally get added automatically. You'll probably need to add it in an
entry point script or a Rakefile. Typically you can do this by
relative-pathing from File.dirname(__FILE__). So say if the script
you're running is in db_all/main.rb, putting

  $LOAD_PATH << File.join(File.dirname(__FILE__), "lib")

near the start of the script should allow you to later

  require 'h2-1.1.1.106'

/Nick

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

    http://xircles.codehaus.org/manage_email
Paul F. (Guest)
on 2009-05-17 08:10
(Received via mailing list)
On Fri, May 15, 2009 at 8:26 AM, Paul F. <removed_email_address@domain.invalid>
wrote:
>>>> I have tried "require", "include_package", "include_class" but none seem
>>> Hope that answers the question,
>>  in netbeans  Project Properties > Java > JRuby Classpath (tab)
> relative-pathing from File.dirname(__FILE__). So say if the script
> you're running is in db_all/main.rb, putting
>
>   $LOAD_PATH << File.join(File.dirname(__FILE__), "lib")
>
> near the start of the script should allow you to later
>
>   require 'h2-1.1.1.106'
>
> /Nick
>
Nick,

After some research it seems my problem might relate to
http://jira.codehaus.org/browse/JRUBY-2495
The jar is installed in the project lib directory.
This code works when the jar is on the classpath (set in netbeans) and
fails when the require method is used.
When the jar is not on the classpath and require 'h2-1.1.106' is used,
the error relates to a driver not being found.
Also fails with the Vladimar suggestion re the  ContextClassLoader
Thread
Could this still a problem related to
http://jira.codehaus.org/browse/JRUBY-2495 ?

require 'rubygems'
require 'java'
#require 'jruby'
#java.lang.Thread.currentThread.setContextClassLoader(JRuby.runtime.jruby_class_loader)

require 'sequel'
#require 'h2-1.1.106'

$LOAD_PATH.unshift File.dirname(__FILE__)

connection_str = "jdbc:h2:db1;USER=sa"
begin
  db = Sequel.connect(connection_str)
  db.test_connection
  puts "Open Database OK"
rescue
  puts "Open Database Failed = #{connection_str}"
  nil
end

Thanks
Paul

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

    http://xircles.codehaus.org/manage_email
Nick S. (Guest)
on 2009-05-17 21:58
(Received via mailing list)
On Sat, May 16, 2009 at 11:08 PM, Paul F. <removed_email_address@domain.invalid>
wrote:
> Could this still a problem related to
> $LOAD_PATH.unshift File.dirname(__FILE__)
>
> connection_str = "jdbc:h2:db1;USER=sa"
> begin
>  db = Sequel.connect(connection_str)
>  db.test_connection
>  puts "Open Database OK"
> rescue
>  puts "Open Database Failed = #{connection_str}"
>  nil
> end

We have to jump through some hoops with AR-JDBC in order to use jar
files require'd by JRuby -- perhaps you're running into this issue.
The issue is the following:

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/D...

"When the method getConnection is called, the DriverManager will
attempt to locate a suitable driver from amongst those loaded at
initialization and those loaded explicitly using the same classloader
as the current applet or application."

The problem is that any drivers require'd by JRuby end up in a child
classloader of the one that started JRuby, so DriverManager doesn't
see them. The workaround (which may require patching Sequel) is to do
something like the following [1]:

      def connection(url, user, pass)
        Jdbc::DriverManager.getConnection(url, user, pass)
      rescue
        # bypass DriverManager to get around problem with dynamically
loaded jdbc drivers
        props = java.util.Properties.new
        props.setProperty("user", user)
        props.setProperty("password", pass)
        create.connect(url, props)
      end

      def create
        driver_class.new
      end

/Nick

[1]:
http://github.com/nicksieger/activerecord-jdbc-ada...

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

    http://xircles.codehaus.org/manage_email
Paul F Fraser (Guest)
on 2009-05-18 06:33
(Received via mailing list)
Nick S. wrote:
>> error relates to a driver not being found.
>> #require 'h2-1.1.106'
>>  nil
> attempt to locate a suitable driver from amongst those loaded at
>       rescue
>       end
>
> /Nick
>
> [1]: 
http://github.com/nicksieger/activerecord-jdbc-ada...
>
>
Nick,

The plot thickens..
When attempting to try your suggestion, I discovered that the driver is
found when debugging the code and not found during normal run if the
jdbc-h2 gem is installed.  This is with no reference to your suggested
code at all.
Sequel (internally) requires 'h2' if the gem is installed.
It seems that the debugger (in netbeans) does clever things with the
driver when accessing the jdbc-h2 gem.
The debugger might be putting the jar on the classpath. I will try and
check if this is happening.

Paul



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

    http://xircles.codehaus.org/manage_email
Alwyn Schoeman (Guest)
on 2012-01-12 21:26
(Received via mailing list)
Hi Nick,

I'm actually having the same problem using dbd-jdbc and jdbc-jtds.  In
my
setup I am using bundler in standalone mode to add the necessary gems to
my
load path and launching my application.  When I use jruby.exe to launch
the
application the driver is found, but when I do 'java -classpath
jruby-complete.jar org.jruby.Main a.rb it doesn't find the driver.  I'm
inclined to agree with you that it is a classloader issue.

Seeing that you are one of the owners of dbd-jdbc, doesn't it make sense
to
put some magic in there seeing that its probably a common problem for
jdbc?



--
View this message in context:
http://ruby.11.n6.nabble.com/Jars-in-Gems-tp346522...
Sent from the JRuby - User mailing list archive at Nabble.com.
Alwyn Schoeman (Guest)
on 2012-01-12 22:32
(Received via mailing list)
Came across  http://jira.codehaus.org/browse/JRUBY-2495 #JRUBY-2495

If it made it into jruby then I don't see why the jtds driver can't be
found
by DriverManager or am I missing something?

--
View this message in context:
http://ruby.11.n6.nabble.com/Jars-in-Gems-tp346522...
Sent from the JRuby - User mailing list archive at Nabble.com.
Alwyn Schoeman (Guest)
on 2012-01-12 22:42
(Received via mailing list)
Found that adding -Xbootclasspath/a:..\lib\jruby-complete.jar;.\bundle
solved
it in my environment.  Not sure what side-effects it has though.

--
View this message in context:
http://ruby.11.n6.nabble.com/Jars-in-Gems-tp346522...
Sent from the JRuby - User mailing list archive at Nabble.com.
This topic is locked and can not be replied to.