简体   繁体   中英

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM