2011-09-02 80 views
2

是否可以使用Java中的ScriptEngineManager設置我的jruby的gem路徑?問題是我正在使用壓縮的jruby解釋器(jruby-complete.jar),並且無法使用預先安裝的gem來獲取此包,所以我正在嘗試使用ScriptEngineManager從Java運行jruby並重定向寶石的路徑,我已經安裝在我的硬盤上。從ScriptEngineManager eval方法設置gem路徑

例子:

public class Main { 
    public static void main(String[] args) { 
     StringBuffer jruby = null; 
     ScriptEngine runtime = null; 

     try { 
      runtime = new ScriptEngineManager().getEngineByName("jruby"); 
      jruby = new StringBuffer(); 

      jruby.append("require 'ruby/Libraries.rb'"); 
      jruby.append("\r\n"); 
      jruby.append("if __FILE__ == $0"); 
      jruby.append("\r\n"); 
      jruby.append("\tzkan = ZKANWritter.new(\""); 
      jruby.append("G:/path/output.xls"); 
      jruby.append("\",\""); 
      jruby.append("G:/path/kanban.txt"); 
      jruby.append("\",\"//path/LT CUU.tab\")"); 
      jruby.append("\r\n"); 
      jruby.append("\tzkan.write(2)"); 
      jruby.append("\r\n"); 
      jruby.append("end"); 

      runtime.eval(jruby.toString()); 
     } catch (Exception ex) { 
      System.out.println("Error: " + ex.getMessage()); 
     } 
    } 
} 

由於提前

〜埃德爾醌

回答

2

我剛剛設置的變量RUBYLIB & GEM_HOME編程紅寶石環境:

ENV["RUBYLIB"] = "lib/libs" 
ENV["GEM_HOME"] = "lib/gems" 

require "library-1.rb" 
... 
require "library-n.rb"