Search code examples
javarubyscriptingjruby

How to run an existing Ruby script in a Java project using JRuby?


I am trying to run an existing Ruby script in a Java project with JRuby. All the examples I have found write a Ruby script on the fly and run it like so:

ScriptingContainer container = new ScriptingContainer();
container.runScriptlet("puts 'hello world'");

However I would like to run an existing Ruby script. Haven't had any luck. Here's what I've been trying:

ScriptingContainer container = new ScriptingContainer();
container.runScriptlet("/path/to/file/test.rb");

My test.rb file looks like:

#!/usr/bin/env jruby

puts "hi"

Solution

  • As you can see from the API docs, runScriptlet has several forms; if you want it to execute a file, it needs two parameters; in your case,

    container.runScriptlet(PathType.ABSOLUTE, "/path/to/file/test.rb");
    

    tl;dr: When in doubt, check the docs.