# Ruby one-file launcher that sets up environment?

**URL:** <https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054>\
**Category:** Ruby\
**Created:** [May 23, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054 "2010-05-23T13:29:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 23, 2010, 1:29pm UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054/1 "2010-05-23T13:29:40Z")

</div>

Hello

I am trying to run some JRuby script and I have to set the CLASSPATH  
for my script to work.

I use a launcher locally so I can run the script as

./scripts/mylauncher script.rb

but if I wanted to distribute a sample script this is somewhat  
troublesome.

I tried to embed the ruby code in a shell script but as Ruby refuses  
to read the code from stdin this is not quite nice:

#!/bin/sh  
CLASSPATH= jruby -e ’  
  
’ “$@”

Note that while this example uses JRuby there might be native  
extensions which require specific environment settings and woud have  
the same launcher requirements.

If somebody had a better idea for a launcher it would be nice.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 23, 2010, 1:55pm UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054/2 "2010-05-23T13:55:25Z")

</div>

Michal S. wrote:

> I am trying to run some JRuby script and I have to set the CLASSPATH  
> for my script to work.  
> […]  
> Note that while this example uses JRuby there might be native  
> extensions which require specific environment settings and woud have  
> the same launcher requirements.

To rephrase: Your software requires some environment variables to be set  
in order to run. The standard way of providing this is is to install a  
script with the executable bit set in /etc/profile.d/ that modifies the  
environment variables on login, e.g.:

/etc/profile.d/mypackage.sh:

export CLASSPATH="$CLASSPATH:/additional/class/paths"

/etc/profile is read on login and should run any executable scripts it  
finds in /etc/profile.d/.

Hope this helps,  
Henning

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 24, 2010, 12:19am UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054/3 "2010-05-24T00:19:58Z")

</div>

On 23 May 2010 13:55, Henning B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in order to run. The standard way of providing this is is to install a  
> Hope this helps,  
> Henning

Well, actually it does not.

If I wanted to distribute the script in multiple files then  
distributing it with a launcher would be just fine and would not  
require the user of the script to have administrative privileges to  
install the script in a system directory nor the additional knowledge  
how to make it effective without restarting his session.

However, I was aiming for one self-contained example which is easy to  
distribute so I wanted one file.

Also I do not want to inflate the environment needlessly while the  
script is not in use. It is not a good practice, the environment space  
is limited.

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 25, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054/4 "2010-05-25T23:39:43Z")

</div>

On 25 May 2010 21:56, Charles Oliver N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > distribute so I wanted one file.
> 
> Alternatively you can just throw everything in the same jar if there  
> aren’t file conflicts…

The thing is that I do have to set the CLASSPATH for my application to  
work, and even set LD\_LIBRARY\_PATH in some cases, even if everything  
is in one jar.

Also setting $CLASSPATH does not work anyway:  
java/lang/ClassLoader.java:1649:in `loadLibrary': java.lang.UnsatisfiedLinkError: Can't load library: /InsightToolkit.jar must be in CLASSPATH/libSwigRuntimeJava.so (NativeException) from java/lang/Runtime.java:787:in `load0’  
from java/lang/Runtime.java:775:in `load' from InsightToolkit/itkbase.java:60:in `’  
from InsightToolkit/itkImageFileReader\_2DJNI.java:1493:in  
`<clinit>' from InsightToolkit/itkImageFileReaderF2.java:67:in `itkImageFileReaderF2\_New’  
from ./testscript.rb:29

Thanks

Michal

---

<div class="post-metadata">

**Author:** ![Michal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/779978/32.png) [@Michal\_S](https://www.ruby-forum.com/u/Michal_S)\
**Post date:** [May 25, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/ruby-one-file-launcher-that-sets-up-environment/189054/5 "2010-05-25T21:57:16Z")

</div>

On Sun, May 23, 2010 at 5:18 PM, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Well, actually it does not.
> 
> If I wanted to distribute the script in multiple files then  
> distributing it with a launcher would be just fine and would not  
> require the user of the script to have administrative privileges to  
> install the script in a system directory nor the additional knowledge  
> how to make it effective without restarting his session.
> 
> However, I was aiming for one self-contained example which is easy to  
> distribute so I wanted one file.

You can generally get by using the runtime CLASSPATH in JRuby:

require ‘java’

$CLASSPATH \<\< ‘somefile.jar’

This doesn’t always work, since it’s using a child classloader in  
JRuby to handle all the classpath entries, but it often works well  
enough for a simple self-contained app.

Alternatively you can just throw everything in the same jar if there  
aren’t file conflicts…

- Charlie
