Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RubyMethodError-从Java调用ruby方法_Java_Ruby_Jruby - Fatal编程技术网

RubyMethodError-从Java调用ruby方法

RubyMethodError-从Java调用ruby方法,java,ruby,jruby,Java,Ruby,Jruby,我有一个ruby类: require 'stringio' require 'hirb' class Engine def initialize() @binding = Kernel.binding end def run(code) # run something stdout_id = $stdout.to_i $stdout = StringIO.new cmd = <<-EOF $SAFE = 3 $st

我有一个ruby类:

require 'stringio'
require 'hirb'

class Engine
  def initialize()
    @binding = Kernel.binding
  end
  def run(code)
    # run something
    stdout_id = $stdout.to_i
    $stdout = StringIO.new
    cmd = <<-EOF
    $SAFE = 3
    $stdout = StringIO.new
    begin
      #{code}
    end
    EOF
    begin
      result = Thread.new { Kernel.eval(cmd, @binding) }.value
    rescue SecurityError
      return "illegal"
    rescue Exception => e
      return e
    ensure
      output = get_stdout
      $stdout = IO.new(stdout_id)
    end

    return output
  end

   private
   def get_stdout
     raise TypeError, "$stdout is a #{$stdout.class}" unless $stdout.is_a? StringIO
     $stdout.rewind
     $stdout.read
   end
end
你知道有什么问题吗


[编辑]在我扩展了内核模块并添加了它工作的命令之后。

NoMethodError的一个可能来源是语法差异。您可能试图使用不同于所需的参数调用方法。确保您正在执行的Ruby版本和方法调用处于同步状态。如果我使用错误的参数调用方法,IRB应返回具有正确调用形式的消息,并且run方法应捕获该消息
public class MyClass {
  private final static String jrubyhome = "/usr/lib/jruby/";
  private String rubySources;
  private String hirbSource;
  private String myEngine;
  private boolean loaded = false;

  private void loadPaths() {
    String userDir;

    userDir = System.getProperty("user.dir");
    rubySources = userDir + "/../ruby";
    hirbSource = userDir + "/hirb.rb";
    myEngine = rubySources + "/engine.rb";

    System.setProperty("jruby.home", jrubyhome);
    System.setProperty("org.jruby.embed.class.path", rubySources+":"+hirbSource);
    System.setProperty("hbase.ruby.sources", rubySources+":"+hirbSource);
  }

  private String commandResponse(String command) 
     throws FileNotFoundException
  { 
    String response;

    loadPaths();

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("jruby");
    ScriptingContainer container = new ScriptingContainer();

    Reader reader = new FileReader(myEngine);

    try {
      Object receiver = engine.eval(reader);
      String method = "run";
      Object ob  = container.callMethod(receiver,method,command);
      response = ob.getClass().toString();

      return response;
    } catch (ScriptException e) {
      System.out.println("exception");
    } 

    return "FAILED";
  }

  public static void main(String args[]) 
    throws IOException {
     MyClass my = new MyClass();
     System.out.println(my.commandResponse(args[0]));
  }  
}