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?

user081608
  • 1,093
  • 3
  • 22
  • 48
  • 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? – the Tin Man Feb 13 '17 at 00: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 – Guilherme Feb 13 '17 at 02:54
  • @Guilherme how would I check if they are the same version? – user081608 Feb 13 '17 at 03:01
  • @user081608 can you print the output of Runtime.getRuntime().exec("export") and Runtime.getRuntime().exec("ruby -v") ? – Guilherme Feb 13 '17 at 03: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? – user081608 Feb 13 '17 at 03:28

1 Answers1

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

Community
  • 1
  • 1
vstrom coder
  • 297
  • 1
  • 8