0

I am trying to run a Ruby script from Java by doing the following:

public static void main(String[] args) {
    try {
        Process process = Runtime.getRuntime().exec("ruby /path/to/file/file.rb");
        process.waitFor();
        System.out.println(process.exitValue()); 
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
}

When I this is run, I get 1 as the exit value. In my Ruby script, I am using Watir and Nokogiri thus a browser should be used. Nothing happens.

If I copy ruby /path/to/file/file.rb into my terminal, it runs correctly. Any reason why this isn't working correctly?

5
  • 1
    When the Java runs, is it running from your account with your permissions, or is it running as another, different, user? Are permissions correct for that user? Does Ruby know where its libraries/gems are? Commented Feb 13, 2017 at 0:40
  • Is the ruby version the same as the one that you run on the terminal? You have to make sure that the environment (environment variables, ruby version loaded, gem set (if any)) is the same in the java runtime process Commented Feb 13, 2017 at 2:54
  • @Guilherme how would I check if they are the same version? Commented Feb 13, 2017 at 3:01
  • @user081608 can you print the output of Runtime.getRuntime().exec("export") and Runtime.getRuntime().exec("ruby -v") ? Commented Feb 13, 2017 at 3:22
  • It looks like when I run that I get version 2.0.0p648 while my terminal is version 2.4.0p0. How would these be different? Does the JVM have its own Ruby? Commented Feb 13, 2017 at 3:28

1 Answer 1

3

You need to consume the standard output and standard error streams to get output from your process and find out what is wrong. See the answer to "Using a thread to capture process output".

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

Comments

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.