Loading ruby files from a jar

Hi,

I am having problem with JRuby not being able to find ruby classes
inside jar files. Here is what I am doing.
I add the gem.jar file’s path to the $: variable. The gem.jar file
contains this file “bundler_gems/gems/rails-3.0.pre/lib/rails.rb”. But,
when I try to require the rails class “require ‘rails’” I get a no such
file to load – rails (LoadError). Is this the wrong way to load a ruby
file from a jar file? If it would be great if you could tell me how to
do it correctly.

Thanks,

Josh


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On 12 Nov 2009, at 14:04, joshmoore wrote:

Hi,

I am having problem with JRuby not being able to find ruby classes
inside jar files. Here is what I am doing.
I add the gem.jar file’s path to the $: variable.

I think most of this will work, but use the $CLASSPATH. For example:

$ echo ‘puts “hello”’ > test.rb
$ jar cf check.jar test.rb
$ cat verify.rb
require ‘java’
$CLASSPATH << ‘check.jar’
require ‘test’
$ rm ‘test.rb’ # No cheating!
$ jruby verify.rb
hello

Damian


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Damian,

Thanks for the help! That does work great. I can access the rails file
now. If I specify the path within the jar file. In my example the
rails class is located here:

bundler_gems/gems/rails-3.0.pre/lib/rails.rb
and if I do “require ‘bundler_gems/gems/rails-3.0.pre/lib/rails’” it
will load the file fine. But, I would like to make it so that I can
just have “require ‘rails’”, but this fails because it cannot file the
rails file. I have tried to put $LOAD_PATH.unshift <<
‘bundler_gems/gems/rails-3.0.pre/lib/’ but this does not help this
situation. Do you know of any way to be able to do this? (require
‘rails’ when it is not in the root directory of the jar file)

Thanks,

Josh

Josh,

Firstly $LOAD_PATH.unshift << ‘PATH’ is redundant. The method ‘unshift’
does the same thing as ‘<<’ (so you could do either $LOAD_PATH.unshift
“VALUE”
or $LOAD_PATH << “VALUE”).

In regards to your issue, have you looked at
http://blog.nicksieger.com/articles/2009/01/10/jruby-1-1-6-gems-in-a-jar?
It outlines how to put gem files in a jar, which Jruby automatically
detects (I believe it is looking for directories named ‘specifications’
in
the classpath).

If that isn’t what you are looking for, we do manual loading of non-gem
libraries from jar files all the time. Typically via something like:
$LOAD_PATH.unshift “file:foo.jar!/fu/bar”
require ‘fubar’ # This is a script in the /fu/bar directory of
foo.jar.

Thanks,
Ben Christenson
Kinetic Data Inc.

Um…I’m a Ruby youngster, so I’m probably wrong but…

My understanding is that unshift prepends values to a sequence, whereas
<< appends to the sequence, and since lookup order can be significant in
the interpretation of $LOAD_PATH (or CLASSPATH, etc), it would seem to
be important to use the proper one to get the desired effect.

Most of the time I use unshift, because I want my directories to be
first in line for consideration, and I any identically named files
located along $LOAD_PATH to resolve to my versions.

If my understanding is incorrect, please let me know.

Steve

Steve,

Nope, you are not wrong. $LOAD_PATH is an Array, and the ‘unshift’ and
‘<<’
methods prepend and append respectively.

$LOAD_PATH (which is also referenced by $:), very similarly to an OS
path
environment variable and looks for files to load starting at the
beginning
of the $LOAD_PATH and progressing to the end of $LOAD_PATH.

Thanks for the correction,
Ben Christenson
Developer/Analyst

Kinetic Data, Inc.
“Building a Better Service Experience”

651.556.0937 I www.kineticdata.com http://www.kineticdata.com

You really just want to require the environment file generated by
bundler

On Thursday, November 12, 2009, joshmoore [email protected] wrote:

On 12 Nov 2009, at 14:04, joshmoore wrote:
$ cat verify.rb
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Josh, just an FYI here:

This issue in JIRA http://jira.codehaus.org/browse/JRUBY-3299 [
http://jira.codehaus.org/browse/JRUBY-3299] is going to get in your way
if
any of the jars you’re requiring have other jars embedded within them.

Not sure if this applies to what you’ve got, but be aware.

the appengine-tools jar up the bundled gems, and require them for you.

You use bundler, (I think that you try to start rails 3 on GAE)
so you need only in config.ru add this:

$LOAD_PATH << ‘bundler_gems’
require ‘environment’

It will add to load path bundled libraries .

Paul

joshmoore-3 wrote:

it correctly.


View this message in context:
http://old.nabble.com/loading-ruby-files-from-a-jar-tp26319226p26689881.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Actually, I pushed out a minor fix (without bumping the version), so
you may need to ‘sudo gem uninstall appengine-tools’ then install it
again with ‘sudo gem install appengine-tools’


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks, I will notice it.

woodie wrote:


View this message in context:
http://old.nabble.com/loading-ruby-files-from-a-jar-tp26319226p26697385.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Actually, I pushed out a minor fix (without bumping the version), so
you may need to ‘sudo gem uninstall appengine-tools’ then install it
again with ‘sudo gem install appengine-tools’


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Yes, but for last version it generates not valid load_pathes, so you
need
make yourselves, while not fixed in appengine-tools.

For example it generate: ‘/bundler_gems/…’

woodie wrote:

require ‘environment’
inside

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


View this message in context:
http://old.nabble.com/loading-ruby-files-from-a-jar-tp26319226p26695697.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email