[kepler-dev] Running Shell Scripts inside Kepler

Robin Schroeder Robin.Schroeder at asu.edu
Thu Apr 7 10:09:34 PDT 2005

Hi Kepler-Dev, 
Have you considered running R in Batch mode?
I have run it both in Windows and Linux from Java's runtime.exec without issue under Java SDK 1.5 in kepler and as a webservice.

Something like this:

//Make the batch command
String[] batchCmd = new String[5];
batchCmd[0] = "/usr/local/R-2.0.0/bin/R";
batchCmd[1] = "CMD";
batchCmd[2] = "BATCH";
batchCmd[3] = "/usr/local/batchFiles/batch1.txt";
batchCmd[4] = "/usr/local/batchFiles/batch1.out";

Process listener = Runtime.getRuntime().exec(batchCmd, envars);


R is a shell script. 
I have attached a copy of an example batch.txt and batch.out, if you are interested.

Each time you run java runtime.exec(), each instance begins a new unrelated thread. You can't really treat it like a command line interface. You can't run an R workflow in unrelated threads. R stores variables in memory during the execution of multiple commands. In order to run more than one command in R using the runtime.exec(), you need to use a batch file. I tried this a thousand different ways. I do a very similar thing with grass as well and it works great. :-)

hope this helps, robin

> Robin Tori Schroeder (formerly Schoeninger)
> International Institute for Sustainability 
> P.O. Box 873211
> Arizona State University
> Tempe, Arizona 85287-3211
> Phone: (480) 727-7290

-----Original Message-----
From: kepler-dev-bounces at ecoinformatics.org
[mailto:kepler-dev-bounces at ecoinformatics.org]On Behalf Of Tobin Fricke
Sent: Wednesday, April 06, 2005 9:50 AM
Cc: Kepler-Dev
Subject: Re: [kepler-dev] Running Shell Scripts inside Kepler

On Wed, 6 Apr 2005, Dan Higgins wrote:

> Now here is where I ran into trouble!!! On Linux and the Mac, 'R' is a
> shell script, not a simple executable! And apparently, the Java

The JavaDoc for the 'Process' class contains the following note:

  The Runtime.exec methods may not work well for special processes on
  certain native platforms, such as native windowing processes, daemon
  processes, Win16/DOS processes on Microsoft Windows, or shell scripts.
  The created subprocess does not have its own terminal or console. All
  its standard io (i.e. stdin, stdout, stderr) operations will be
  redirected to the parent process through three streams
  (Process.getOutputStream(), Process.getInputStream(),
  Process.getErrorStream()). The parent process uses these streams to feed
  input to and get output from the subprocess.  Because some native
  platforms only provide limited buffer size for standard input and output
  streams, failure to promptly write the input stream or read the output
  stream of the subprocess may cause the subprocess to block, and even

In my test, executing a shell script worked without problem:

[tobin at robin ~/proving]$ ls
foo.sh  RunShellScript.class  RunShellScript.java

[tobin at robin ~/proving]$ cat foo.sh
echo Hello, World!

[tobin at robin ~/proving]$ cat RunShellScript.java

/* Tobin Fricke

class RunShellScript {
  public static void main(String args[]) {
    try {
      Process p = Runtime.getRuntime().exec("./foo.sh");
      /* Note that 'input' and 'output' are from the perspective of the
         controlling Java class.  This 'input stream' refers to the process's
         stdout! */
      java.io.InputStream o = p.getInputStream();
      /* Wait for the process to exit. */
      /* Collect its output. */
      int l = o.available();
      byte[] b = new byte[l];
      o.read(b, 0, l);
      System.out.print(new String(b));
    } catch (Exception e) {
      System.out.println("Woops! " + e);
[tobin at robin ~/proving]$ javac RunShellScript.java
[tobin at robin ~/proving]$ java RunShellScript
Hello, World!
[tobin at robin ~/proving]$

I'd make sure that the R shell script starts with the proper "#!" magic
cookie & interpreter signture.  Also, does anyone know how to simply dump
the 'InputStream' obtained from the process to the Java stdout without
doing the silly byte[] --> String business I did?



Kepler-dev mailing list
Kepler-dev at ecoinformatics.org
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 1112380112537_batch.txt
Url: http://mercury.nceas.ucsb.edu/ecoinformatics/pipermail/kepler-dev/attachments/20050407/39b1d0bf/1112380112537_batch.txt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1112380112537_batchOutput.out
Type: application/octet-stream
Size: 1825 bytes
Desc: 1112380112537_batchOutput.out
Url : http://mercury.nceas.ucsb.edu/ecoinformatics/pipermail/kepler-dev/attachments/20050407/39b1d0bf/1112380112537_batchOutput.obj

More information about the Kepler-dev mailing list