0

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 ?

3
  • 1
    Do you get any exceptions? What happens? Just nothing? Commented Jul 7, 2014 at 10:54
  • Not getting any exceptions also . Commented Jul 7, 2014 at 10:55
  • 2
    try reading Process's error stream/out stream for more insight to what is going wrong. Commented Jul 7, 2014 at 11:00

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Added your code snippet. The commands are getting printed but the script down't give the output.
Are you expecting some output or something should change in the enviorment.Give a brief description what is the supposed effect of your scipt.
Sure, the java code gets a set of images while the ruby code processes the image to give a combined image
Try debugging the code in your ruby script, put some puts statements to see the execution is going as expected when you run the script from java. My guess is that some filepaths are getting messed up.
Yes, it was an issue with the file paths. Added absolute value of the path before script and it worked.
1

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();
    }
  }

4 Comments

Tried the code snippet. Same result :( THe ruby code has no issues as I get correct results when I run it from command line
Exit value 0 means normal execution of program. Your program is executed successfully.
But, the script is not executed
Yeah that is the issue, Try below steps. 1. Create a bat file which internally run ruby file. 2. Execute bat file from java code. Let see this time it works or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.