anirudh_raja
anirudh_raja

Reputation: 379

Calling a ruby script from a java code

I'm trying to call a ruby script from within a java code. The script.rb file in in the same folder as the java code.

try 
{
      Process p = Runtime.getRuntime().exec("ruby script.rb");
} 
catch (IOException e)
{
  e.printStackTrace();
}

However, the script doesn't get executed. Any suggestions on what can be done ?

Upvotes: 0

Views: 4422

Answers (2)

hahcho
hahcho

Reputation: 1409

import java.io.*;

public class RubyCall {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("ruby script.rb");
            process.waitFor();

            BufferedReader processIn = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = processIn.readLine()) != null) {
                System.out.println(line);
            } 
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I think you just forgot that the process output is not the systems output. With this code you would see the process output printed on standard out and see if there are any errors.

Upvotes: 2

Rais Alam
Rais Alam

Reputation: 7016

Try below code. wait for process to execute and check the out of the logic written in ruby file.

public static void main(String argv[]) {
    try {

      Process p = Runtime.getRuntime().exec("ruby script.rb");
      p.waitFor();
      System.out.println(p.exitValue());
    }
    catch (Exception err) {
      err.printStackTrace();
    }
  }

Upvotes: 1

Related Questions