2013-01-17 32 views
0

我有一段Ruby代码,它依赖于从C构建的二进制文件。我通常通过反引号来调用二进制代码。但是现在,当我使用Warbler将Ruby代码打包到jar中时,我不确定我将如何访问二进制文件。在Warbler中,如何通过Ruby代码访问编译的二进制文件?

我的代码结构如下:

root/ 
    |--bin/ 
     |--exec.rb #This is the executable when I call java -jar example.jar 
    |--lib/ 
     |--Module1.rb #This dir contains all the ruby modules my code requires 
    |--ext/ 
     |--a.out  #A binary compiled with gcc 
    |--.gemspec  #A file to guide warbler into building this structure into a jar 

我以前warble建立这个整体结构成一个罐子。在Ruby中,我可以通过exec.rb中的以下语句访问我的a.out

exec = "#{File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), 'ext'))}/a.out}"; 
`exec` 

但当我尝试这个代码打包为一个jar我得到以下错误:

/bin/sh: file:/path/to/my/jar/example.jar!/root/ext/a.out: not found 

那么,如何访问封装在一个罐子里的可执行文件。

回答

0

jar置于lib文件夹中。

需要它的代码

require 'java' 
Dir["#{File.expand_path(File.join(Rails.root, 'lib'))}/\*.jar"].each { |jar| require jar } 
# A war is treated as a directory. If that is not successful add the lib folder to the CLASSPATH environment variable 

然后,它应该可以被使用。

编辑:

也许这就是你正在寻找https://stackoverflow.com/a/600198/643500你可以使用JRuby实现它。

+0

我曾尝试类似的东西,但它没有奏效。我认为我的问题很模糊,所以我用更多背景来重述我的问题。 – arrac

+0

感谢您的链接。我会尽力给它一个镜头。 – arrac

相关问题