Including gems in a bundle with Rawr (Jruby)

I'm able to make a jar/.exe/.app out of a simple script with Rawr, but
I can't seem to run one that uses a gem. It complains that it can't
find the gem at runtime. I've read
<http://kenai.com/projects/monkeybars/pages/UseRubyGemsInYourApplication>
and tried to follow the instructions, but without success. Might
anyone be able to help?

I'm not sure if I should just paste my source files here, or use a
pastebin or something. The script is very short, but everything taken
together still requires scrolling. Please let me know how I should
post my code. Thanks.

Thanks for your response.

Eric Christopherson wrote:

I'm able to make a jar/.exe/.app out of a simple script with Rawr, but
I can't seem to run one that uses a gem. It complains that it can't
find the gem at runtime. I've read
<http://kenai.com/projects/monkeybars/pages/UseRubyGemsInYourApplication&gt;
and tried to follow the instructions, but without success. Might
anyone be able to help?

Is the gem installed on all system were the rawr-packaged app will run?

No; the reason I want to use Rawr is to deliver a program where no
Ruby installation exists.

If you are not assured of that then having code that expects a gem is asking
for trouble.

Also, the instructions on the page you gave has this:

"You need to unpack the gem into lib/ruby"

The reason for that is so that you do not need to use rubygems to load
anything. You should be using 'require <filename>', and all paths should be
relative to your app. rubygems messes with the load path, making life
difficult for self-contained apps.

OK. I was able to get it to load Barby, but I had to add
'lib/ruby/barby/lib' to my $LOAD_PATH. I don't understand the purpose
of unpacking the gem into lib/ruby specifically, when it could reside
anywhere within the hierarchy.

If you have code that is attempting to load a gem instead of trying to load
files out of your bundled app then something is likely wrong.

(Note that the example code on that help page never makes use of rubygems.
Only plain old 'require'.)

Some gems contain code that makes a call to "require 'rubygems'". (There was
a previous thread on ruby-talk about how inane this is.)

Yes, and I see now that prawn (which I also need) does just that. :frowning:

I usually hate to tell people "don't do that" when they ask how do do
something, but trying to use rubygems from inside an app packaged up with
rawr is just asking for trouble. One of the purposes of rawr is create
self-sufficient apps.

That's what I want to do: make a program (that uses some gems) and
package them all together so they don't need to be installed
separately. I must be misunderstanding the intention of the page we're
discussing; I thought it was about doing that.

···

On Sun, Feb 21, 2010 at 3:32 PM, James Britt <james.britt@gmail.com> wrote:

Grep you code for any reference to rubygems and delete it. :slight_smile:

Eric Christopherson wrote:

OK. I was able to get it to load Barby, but I had to add
'lib/ruby/barby/lib' to my $LOAD_PATH. I don't understand the purpose
of unpacking the gem into lib/ruby specifically, when it could reside
anywhere within the hierarchy.

Wherever you want to place it, you have to be sure that the load path includes it. The rawr build_config has lib/ruby by default, I believe. (You need to indicate what local paths to set for $: in your code, but also what folders should be packaged up by rawr, and how they should be structured in any resulting jar file.)

Technically, you can set this up as you like, but it's far easier to use and to explain using some rawr conventions then saying, abstractly, you can set up your sub-dirs and load path almost any way you like. Because, as it happens, you have to be careful, since (as the page explains) code inside gems sometimes makes assumptions about it's parent directories and such.

For example, I had trouble using the 'builder' code. It came down to there being 2 builder.rb files. One would load the other. But if $: is not set up correctly, then the wrong one may be found first, with annoying results. (And I'm pretty sure I wrote an earlier version of the page under discussion precisely because of these issues.)

If you have code that is attempting to load a gem instead of trying to load
files out of your bundled app then something is likely wrong.

(Note that the example code on that help page never makes use of rubygems.
Only plain old 'require'.)

Some gems contain code that makes a call to "require 'rubygems'". (There was
a previous thread on ruby-talk about how inane this is.)

Yes, and I see now that prawn (which I also need) does just that. :frowning:

GREG! on noes ... :slight_smile:

I usually hate to tell people "don't do that" when they ask how do do
something, but trying to use rubygems from inside an app packaged up with
rawr is just asking for trouble. One of the purposes of rawr is create
self-sufficient apps.

That's what I want to do: make a program (that uses some gems) and
package them all together so they don't need to be installed
separately. I must be misunderstanding the intention of the page we're
discussing; I thought it was about doing that.

The page is setting up unpacked gem files in an app. (The larger premise of rawr is that the app has a main file, with all other required files being included in subdirectories or jars, all bundled up in such a way that Java can execute it. )

rawr can build installers for your app that makes sure all the related files get put in place.

But you have to unbundle any gems used by your code. And at that point, you should thinking of them as gems, and simply as libs that live in a subdirectory of your application folder.

···

--
James Britt

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
www.neurogami.com - Smart application development

Eric Christopherson wrote:

OK. I was able to get it to load Barby, but I had to add
'lib/ruby/barby/lib' to my $LOAD_PATH. I don't understand the purpose
of unpacking the gem into lib/ruby specifically, when it could reside
anywhere within the hierarchy.

Wherever you want to place it, you have to be sure that the load path
includes it. The rawr build_config has lib/ruby by default, I believe. (You
need to indicate what local paths to set for $: in your code, but also what
folders should be packaged up by rawr, and how they should be structured in
any resulting jar file.)

OK. I put this at the top of my script:

$LOAD_PATH << 'lib/ruby/barby/lib'
$LOAD_PATH << 'lib/ruby/prawn/lib'
$LOAD_PATH << 'lib/ruby/prawn-core/lib'
$LOAD_PATH << 'lib/ruby/prawn-layout/lib'
$LOAD_PATH << 'lib/ruby/prawn-security/lib'

This allows the jarred script to run -- but I later realyzed that it's
still reading the files from the filesystem. If I run the jar from a
different directory, or if I move or delete the lib directory, it is
unable to load them.

Questions:
1. How do I add libraries *inside the jar* to my $LOAD_PATH?
2. Is there a more elegant way to add them than to just list each one
at the top of my script? I thought the manifest.rb file was supposed
to take care of this, but it doesn't.
3. What is actually supposed to go in manifest.rb?

Technically, you can set this up as you like, but it's far easier to use and
to explain using some rawr conventions then saying, abstractly, you can set
up your sub-dirs and load path almost any way you like. Because, as it
happens, you have to be careful, since (as the page explains) code inside
gems sometimes makes assumptions about it's parent directories and such.

For example, I had trouble using the 'builder' code. It came down to there
being 2 builder.rb files. One would load the other. But if $: is not set
up correctly, then the wrong one may be found first, with annoying results.
(And I'm pretty sure I wrote an earlier version of the page under
discussion precisely because of these issues.)

OK.

If you have code that is attempting to load a gem instead of trying to
load
files out of your bundled app then something is likely wrong.

(Note that the example code on that help page never makes use of
rubygems.
Only plain old 'require'.)

Some gems contain code that makes a call to "require 'rubygems'". (There
was
a previous thread on ruby-talk about how inane this is.)

Yes, and I see now that prawn (which I also need) does just that. :frowning:

GREG! on noes ... :slight_smile:

Interestingly, prawn works just fine as long as the prawn directories
are available on the file system -- even though it does a "require
'rubygems'". (I've even uninstalled the actual prawn gems and it still
works.)

···

On Mon, Feb 22, 2010 at 12:28 AM, James Britt <james.britt@gmail.com> wrote:

I usually hate to tell people "don't do that" when they ask how do do
something, but trying to use rubygems from inside an app packaged up with
rawr is just asking for trouble. One of the purposes of rawr is create
self-sufficient apps.

That's what I want to do: make a program (that uses some gems) and
package them all together so they don't need to be installed
separately. I must be misunderstanding the intention of the page we're
discussing; I thought it was about doing that.

The page is setting up unpacked gem files in an app. (The larger premise of
rawr is that the app has a main file, with all other required files being
included in subdirectories or jars, all bundled up in such a way that Java
can execute it. )

rawr can build installers for your app that makes sure all the related files
get put in place.

But you have to unbundle any gems used by your code. And at that point, you
should thinking of them as gems, and simply as libs that live in a
subdirectory of your application folder.

Eric Christopherson wrote:

OK. I was able to get it to load Barby, but I had to add
'lib/ruby/barby/lib' to my $LOAD_PATH. I don't understand the purpose
of unpacking the gem into lib/ruby specifically, when it could reside
anywhere within the hierarchy.

Wherever you want to place it, you have to be sure that the load path
includes it. The rawr build_config has lib/ruby by default, I believe. (You
need to indicate what local paths to set for $: in your code, but also what
folders should be packaged up by rawr, and how they should be structured in
any resulting jar file.)

OK. I put this at the top of my script:

$LOAD_PATH << 'lib/ruby/barby/lib'
$LOAD_PATH << 'lib/ruby/prawn/lib'
$LOAD_PATH << 'lib/ruby/prawn-core/lib'
$LOAD_PATH << 'lib/ruby/prawn-layout/lib'
$LOAD_PATH << 'lib/ruby/prawn-security/lib'

(Disclaimer: This answer is based solely on Ruby knowledge, not JAR behavior or JRuby)

Does it work if you do:

$LOAD_PATH.unshift 'lib/ruby/prawn-security/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-layout/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-core/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn/lib'
$LOAD_PATH.unshift 'lib/ruby/barby/lib'

this puts your directories at the front

-Rob

This allows the jarred script to run -- but I later realyzed that it's
still reading the files from the filesystem. If I run the jar from a
different directory, or if I move or delete the lib directory, it is
unable to load them.

Questions:
1. How do I add libraries *inside the jar* to my $LOAD_PATH?
2. Is there a more elegant way to add them than to just list each one
at the top of my script? I thought the manifest.rb file was supposed
to take care of this, but it doesn't.
3. What is actually supposed to go in manifest.rb?

Technically, you can set this up as you like, but it's far easier to use and
to explain using some rawr conventions then saying, abstractly, you can set
up your sub-dirs and load path almost any way you like. Because, as it
happens, you have to be careful, since (as the page explains) code inside
gems sometimes makes assumptions about it's parent directories and such.

For example, I had trouble using the 'builder' code. It came down to there
being 2 builder.rb files. One would load the other. But if $: is not set
up correctly, then the wrong one may be found first, with annoying results.
(And I'm pretty sure I wrote an earlier version of the page under
discussion precisely because of these issues.)

OK.

If you have code that is attempting to load a gem instead of trying to
load
files out of your bundled app then something is likely wrong.

(Note that the example code on that help page never makes use of
rubygems.
Only plain old 'require'.)

Some gems contain code that makes a call to "require 'rubygems'". (There
was
a previous thread on ruby-talk about how inane this is.)

Yes, and I see now that prawn (which I also need) does just that. :frowning:

GREG! on noes ... :slight_smile:

Interestingly, prawn works just fine as long as the prawn directories
are available on the file system -- even though it does a "require
'rubygems'". (I've even uninstalled the actual prawn gems and it still
works.)

I usually hate to tell people "don't do that" when they ask how do do
something, but trying to use rubygems from inside an app packaged up with
rawr is just asking for trouble. One of the purposes of rawr is create
self-sufficient apps.

That's what I want to do: make a program (that uses some gems) and
package them all together so they don't need to be installed
separately. I must be misunderstanding the intention of the page we're
discussing; I thought it was about doing that.

The page is setting up unpacked gem files in an app. (The larger premise of
rawr is that the app has a main file, with all other required files being
included in subdirectories or jars, all bundled up in such a way that Java
can execute it. )

rawr can build installers for your app that makes sure all the related files
get put in place.

But you have to unbundle any gems used by your code. And at that point, you
should thinking of them as gems, and simply as libs that live in a
subdirectory of your application folder.

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Feb 23, 2010, at 10:28 AM, Eric Christopherson wrote:

On Mon, Feb 22, 2010 at 12:28 AM, James Britt > <james.britt@gmail.com> wrote:

Hi,
i have no manifest file.
I which file i have to place $LOAD_PATH?

Rob Biedenharn wrote:

···

On Feb 23, 2010, at 10:28 AM, Eric Christopherson wrote:

$LOAD_PATH << 'lib/ruby/barby/lib'
$LOAD_PATH << 'lib/ruby/prawn/lib'
$LOAD_PATH << 'lib/ruby/prawn-core/lib'
$LOAD_PATH << 'lib/ruby/prawn-layout/lib'
$LOAD_PATH << 'lib/ruby/prawn-security/lib'

(Disclaimer: This answer is based solely on Ruby knowledge, not JAR
behavior or JRuby)

Does it work if you do:

$LOAD_PATH.unshift 'lib/ruby/prawn-security/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-layout/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-core/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn/lib'
$LOAD_PATH.unshift 'lib/ruby/barby/lib'

this puts your directories at the front

--
Posted via http://www.ruby-forum.com/\.

Does it work if you do:

$LOAD_PATH.unshift 'lib/ruby/prawn-security/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-layout/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn-core/lib'
$LOAD_PATH.unshift 'lib/ruby/prawn/lib'
$LOAD_PATH.unshift 'lib/ruby/barby/lib'

this puts your directories at the front

-Rob

I'm not sure at this point; I seem to have messed something else up in
the script. I am guessing it won't help, though; when I used << to
append the paths, my script ran, but it turned out that it was looking
on the filesystem instead of within the jar.

···

On Tue, Feb 23, 2010 at 9:48 AM, Rob Biedenharn <Rob@agileconsultingllc.com> wrote:

On Fri, Mar 5, 2010 at 5:17 AM, Malte Be <malte.beran@gmail.com> wrote:

Hi,
i have no manifest file.
I which file i have to place $LOAD_PATH?

I placed it in my main script, but then put it in a secondary script
that the main script requires. However, it didn't help.