[英]Executing a Ruby script from Java using JRuby results in 'LoadError: no such file to load …'
I'd like to execute a Ruby script from Java using JRuby (Linux Mint). 我想使用JRuby(Linux Mint)从Java执行Ruby脚本。 I can successfully execute the script from the terminal by calling
ruby script.rb
. 我可以通过调用
ruby script.rb
从终端成功执行脚本。 However, the script contains a require
that causes "no such file to load"
exception when trying to execute it from Java using JRuby: 但是,该脚本包含一个
require
,当尝试使用JRuby从Java执行该"no such file to load"
时,该require
会导致"no such file to load"
异常:
package com.company;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
public class Main
{
public static void main(String[] args)
{
ScriptingContainer ruby;
ruby = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
Object result = ruby.runScriptlet(PathType.ABSOLUTE, "/home/user/Scripts/script.rb");
}
}
The problem seems to be in the require ...
part of the script.rb , since this code works fine for eg a simple script that only displays text. 问题似乎出在script.rb的
require ...
部分中,因为此代码可以很好地工作,例如仅显示文本的简单脚本。
The script uses WinRb , a Ruby library for Windows Remote Management. 该脚本使用WinRb (用于Windows远程管理的Ruby库)。 script.rb :
script.rb :
require 'winrm'
endpoint = 'http://somehost.domain.org:5985/wsman'
krb5_realm = 'DOMAIN.ORG'
command = 'hostname'
winrm = WinRM::WinRMWebService.new(endpoint, :kerberos, :realm => krb5_realm)
winrm.cmd(command) do |stdout, stderr|
STDOUT.print stdout
STDERR.print stderr
end
When I run the Java code I get this exception: 当我运行Java代码时,出现以下异常:
LoadError: no such file to load -- winrm
require at org/jruby/RubyKernel.java:940
<top> at /home/user/Scripts/script.rb:5
Exception in thread "main" org.jruby.embed.EvalFailedException:
(LoadError) no such file to load -- winrm
Here's what I tried: 这是我尝试过的:
require 'rubygems'
to the top of the script. require 'rubygems'
。 This only changes the exception to LoadError: no such file to load -- rubygems
LoadError: no such file to load -- rubygems
require '/var/lib/gems/1.9.1/gems/winrm-1.3.3/lib/winrm'
. require '/var/lib/gems/1.9.1/gems/winrm-1.3.3/lib/winrm'
。 Instead of the original exception, at runtime I now get: LoadError: no such file to load -- date require at org/jruby/RubyKernel.java:940
LoadError: no such file to load -- date require at org/jruby/RubyKernel.java:940
Other: 其他:
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ jruby -v
jruby 9.0.0.0 (2.2.2) 2015-07-21 e10ec96 Java HotSpot(TM) 64-Bit Server VM 25.45-b02
How can I get JRuby to execute this script successfully? 如何获得JRuby来成功执行此脚本?
解决方案是:
jruby -S gem install winrm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.