0

I've been having real problems trying to get a ruby script to run through Java. I've had all kinds of solutions proposed, and all of them are failing for some reason, so I'm trying to simplify my problem.

Let's say I have a shell script that just has this line in it:

ruby -rubygems script/test_s2t.rb

At the terminal, I can run this script using script/runruby.sh and it works as expected. Now let's say I have a Java method that does the following:

String[] cmd = {"script/runruby.sh"};
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();

This doesn't work (it throws an error back from the Ruby script, specifically, but this is a misdirect because it's really down to the script itself not working as expected). My question is not why that test_s2t.rb script doesn't work, because I think that might be distracting me from the real problem.

My question is simply what is different when I run something through ProcessBuilder as opposed to just running it via the command line. Is it a permissions thing? Path differences? There must be something screwing around with the environment the script runs in, because I can't see a problem with the script itself.

As alwyas, any suggestions appreciated. Three days and counting on this issue...

EDIT - For those curious, the exact error I receive in Java is the one described at the bottom of this question: Java receives an error executing Ruby script; Terminal doesn't

The outcome we got in that question was that I should try JRuby, but that resulted in further problems as I can't get the gems to work properly within JRuby. So I went back to asking myself why it wouldn't run normally in the first place.

The reason I think the error is a distraction is because the error is given simply because it processed a string it wasn't expecting to see. The string it expects is the normal process the script runs, which is using ffmpeg and suchlike. What this means is that the script encountered another error (which it isn't showing, which means it was probably not caused by ruby/jruby but by the processes the script launches like ffmpeg).

It's incredibly frustrating, purely because it runs so perfectly from the command line.

Community
  • 1
  • 1
mtrc
  • 1,317
  • 3
  • 16
  • 39
  • While it might be a distraction from the real issue that you percieve, what is the error you get from the script? ProcessBuilder executes processes in the *same* environment as itself – ArjunShankar Mar 28 '12 at 14:04
  • I'll edit in the error, and some explanation. Done. – mtrc Mar 28 '12 at 14:21

1 Answers1

3

I've run into similar problems and there are two things that seem to be common problems:

1) The environment of the child process will be the same as environment of the current virtual machine. This includes the working directory of the launched process.

Example from: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory("myDir");

Alternatively, you could set the environment inside the shell script.

2) Do you have the proper shebang #! at the beginning of the .sh file? Personally I'd make it absolutely clear and perhaps explicitly call bash or zsh or whatever with the path to the shell script as the first argument OR directly call ruby with the '-rubygems' and 'script/test_s2t.rb' as arguments.

Good Luck!

Jeshua
  • 96
  • 3
  • Thanks for these - will try them out tomorrow and get back to you! – mtrc Mar 28 '12 at 22:13
  • Regarding the second suggestion - I actually moved this ruby call into a script so I could simplify the question, previously I'd tried to call Ruby directly, but no dice. So I don't think it's that. I will investigate the local environment though. The working directory is fine, but perhaps some variables are not set... – mtrc Mar 28 '12 at 23:03
  • 1
    You BEAUTIFUL WO/MAN. Oh man. I can't believe that worked. Huge discrepancies between the ProcessBuilder's $PATH and the Mac's $PATH. Once I set the PB's path variable correctly it ran instantly. I can't wait to push on with the project. Thank you so, so much. – mtrc Mar 28 '12 at 23:07