Ruby one-file launcher that sets up environment?

M

Michal Suchanek

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=<something> jruby -e '
<some code that does not use single quotes>
' "$@"

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
 
H

Henning Bekel

Michal said:
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
 
M

Michal Suchanek

Michal said:
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

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
 
C

Charles Oliver Nutter

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
 
M

Michal Suchanek

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...

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 `<clinit>'
from InsightToolkit/itkImageFileReader_2DJNI.java:1493:in `<clinit>'
from InsightToolkit/itkImageFileReaderF2.java:67:in
`itkImageFileReaderF2_New'
from ./testscript.rb:29

Thanks

Michal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top