简体   繁体   English

使用JRuby从Java执行Ruby脚本会导致“ LoadError:没有要加载的此类文件……”

[英]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.rbrequire ...部分中,因为此代码可以很好地工作,例如仅显示文本的简单脚本。

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: 这是我尝试过的:

  1. adding 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
  2. specifying full path: 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.

相关问题 org.jruby.embed.EvalFailedException:(LoadError)没有要加载的文件 - org.jruby.embed.EvalFailedException: (LoadError) no such file to load 运行时vs.JRuby从Java执行Ruby脚本 - Runtime vs. JRuby to execute Ruby script from Java 如何使用JRuby在Java项目中运行现有的Ruby脚本? - How to run an existing Ruby script in a Java project using JRuby? 从Ruby使用Java:JRuby与Rjb - Using Java from Ruby: JRuby vs. Rjb Heroku-如何解决“无法加载此类文件-工头/ cli(LoadError)”的问题? -[Java,Windows,Ruby-on-rails] - Heroku - How to fix “cannot load such file — foreman/cli (LoadError)” ? - [Java, Windows, Ruby-on-rails] 如何使用JRuby在Java中创建Ruby模块? - How to create a Ruby module in Java using JRuby? 在没有JRuby和Java的情况下从Ruby访问JMX - Accessing JMX from Ruby without JRuby and Java 如何从JRuby中ScriptingContainer执行的ruby脚本初始化Java对象? - How to initialize Java objects from a ruby script being executed by ScriptingContainer in JRuby? Sinatra Jruby Heroku - jruby:没有这样的文件或目录 - trinidad(LoadError) - Sinatra Jruby Heroku - jruby: No such file or directory — trinidad (LoadError) 如何使用jruby从Java运行需要Ruby库的Ruby(mcollective) - How to run ruby which require a ruby library(mcollective) from java using jruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM