`
DigitalSonic
  • 浏览: 215238 次
社区版块
存档分类
最新评论

Step by Step-Turning Ruby Files into Java Classes

阅读更多

以前做了个Ruby的项目,最近有了点新的需求,其中有一条是要保护源代码,我选择用JRuby把rb文件编译为class,查了些资料,看了下jrubyc相关的一些代码,也许有朋友有类似的需要,和大家分享一下。突发奇想用英文来写,很久不写英文了,果然这个还得常写写。。。附上对应的中文,如果不高兴看英文的同学可以直接看中文的,呵呵。

 

There are many reasons for us to turn Ruby files (.rb) into Java classes (.class). For example, you want a high-performance system, or you want to protect your ruby code, etc. Last year, JRuby finished its compiler which can help you to do the compilation. Now you’ve got another choice besides XRuby. You can find some material on JRuby’s Wiki, but it’s far from enough, and the most pages you find on the Internet just compile some hello world programs. I tried to compile my ruby project (of course, it's more complex than printing "Hello World", it's a network traffic control script under FreeBSD) last week. If you have the same requirement, maybe my experience can give you a little help :-)
The first question is how to start the compiler. The Wiki just tells us to use "jrubyc". Actually, there's no jrubyc.bat or jubyc.exe in bin at all. So, if you are using Windows like me, you should start the script jrubyc manually. For example:

jruby %JRUBY_HOME%\bin\jrubyc <rb>

Suppose your program is very simple, please forgive me using "Hello World" again. (src\hello.rb)

p "Hello World"

The command to compile it looks like this:

jruby %JRUBY_HOME%\bin\jrubyc src\hello.rb

Then, try this one to run it:

java -cp .;%JRUBY_HOME%\lib\jruby.jar ruby.src.hello

Have you feel something strange? Why the class is ruby.src.hello? After checking the command line parameters, you will find the answer. Here is the command pattern:

jrubyc [options] (FILE|DIRECTORY)

jrubyc has three parameters:

-d,--dir DIR Use DIR as the root of the compiled package and filename
-p,--prefix PREFIX Prepend PREFIX to the file path and package. "ruby" is default
 -t,--target TARGET Output files to TARGET directory

Now, try to compile the hello.rb again:

jruby %JRUBY_HOME%\bin\jrubyc -d src -p "" src\hello.rb
java -cp .;%JRUBY_HOME%\lib\jruby.jar hello

Next, find yourself some other Ruby files which have require or include or something else. Put them into a directory. Let's call it dir. Try the following command:

md classes
jruby %JRUBY_HOME%\bin\jrubyc -t classes -d dir -p "" dir

If everything goes right, you can check the classes directory. All the files are there. Since you require something outside the source files, don’t forget to tell java where to find them. For example, your dir\complex_ruby_script required yaml.

java -cp .\classes;%JRUBY_HOME%\lib\jruby.jar;%JRUBY_HOME%\lib\ruby\1.8 complex_ruby_script

You should see the result of your Ruby program. Maybe I should say your Java program.

That's all. If you want to compile your Rails project so as to deploy it into a Java EE application server, please check out GoldSpike and Warbler.

 

分享到:
评论
3 楼 DigitalSonic 2008-09-08  
这个是针对非WEB的,如果你是WEB项目(也就是如果你是RAILS的),可以使用warbler。
其实如果真是大项目可以像Java中用ant来构建项目那样,自己写rake啊。
2 楼 mogi_1020 2008-08-28  
首先谢谢楼主分享经验!
近期也碰到类似的需求,但是感觉找来找去也找不到一个相对完整的对一个JRUBY工程的.rb文件编译为.class文件的说明。看了楼主的事例以后,我理解仍然是逐个文件的编译!我感觉这样是不是不太科学啊,而且对编译过的文件的结构路径还要重新布置,难免会出现意想不到的后果!请问楼主之后有没有找到比较成熟的对整个工程进行编译的办法啊?
1 楼 DigitalSonic 2008-06-02  
<p>有很多原因促使我们将Ruby文件(.rb)编译成Java的类,可以是追求更高的性能,也可是出于要保护源代码的目的等等。去年,JRuby完成了它的编译器,现在你有了XRuby以外的另一个选择。在JRuby的Wiki上有些材料,不过那还远远不够,而在Internet上找到的大多数东西又只是简单地编译hello world程序。上周我试着编译了一下我的一个ruby项目(当然,它做的事情比打印“Hello World”复杂多了,那是一个FreeBSD下的流量控制脚本)。如果你也有类似的需求,希望我的经验能对你有所帮助:-) <br/><br/>首先是如何启动编译器。Wiki上只是告诉我们使用“jrubyc”。实际上bin目录里根本就没有jrubyc.bat或者是jrubyc.exe。所以,如果你和我一样在使用Windows,那就需要手动启动jrubyc脚本。例如: </p>
<div class='quote_div'>jruby %JRUBY_HOME%\bin\jrubyc &lt;rb&gt; </div>
<p>假设你的程序很简单,请原谅我在这里又一次使用了“Hello World”。 (src\hello.rb)</p>
<pre name='code' class='ruby'>p "Hello World"</pre>
<p>用如下命令进行编译: <br/></p>
<div class='quote_div'>jruby %JRUBY_HOME%\bin\jrubyc src\hello.rb </div>
<p>然后,这样来执行它: </p>
<div class='quote_div'>java -cp .;%JRUBY_HOME%\lib\jruby.jar ruby.src.hello </div>
<p>是不是觉得有点奇怪?为什么这个类是ruby.src.hello?在看过命令行参数后你就知道了。它的命令行格式如下: </p>
<div class='quote_div'>jrubyc [options] (FILE|DIRECTORY) </div>
<p>jrubyc有3个参数: <br/><br/>
</p><table border='0'>
<tbody>
<tr>
<td>-d,--dir DIR</td>
<td>将DIR作为编译后的包和文件的根目录 </td>
</tr>
<tr>
<td>-p,--prefix PREFIX</td>
<td>将PREFIX作为文件或包的路径的前缀。默认是“ruby” </td>
</tr>
<tr>
<td>-t,--target TARGET</td>
<td> 将文件输出到TARGET目录 </td>
</tr>
</tbody>
</table>
<p/>
<p>现在,重新编译一下src\hello.rb: </p>
<div class='quote_div'>jruby %JRUBY_HOME%\bin\jrubyc -d src -p "" src\hello.rb <br/>java -cp .;%JRUBY_HOME%\lib\jruby.jar hello </div>
<p>接下来,找些有require、include或别的什么东西的Ruby文件。把它们放到一个目录里,假设名字是dir。运行如下命令: </p>
<div class='quote_div'>md classes <br/>jruby %JRUBY_HOME%\bin\jrubyc -t classes -d dir -p "" dir </div>
<p>如果一切正常,看一下classes目录,所有的文件都在那里了。因为你在源代码中引用了些别的东西,请一定告诉java到哪里去寻找这些文件。例如dir\complex_ruby_script里require 了yaml。 </p>
<div class='quote_div'>java -cp .\classes;%JRUBY_HOME%\lib\jruby.jar;%JRUBY_HOME%\lib\ruby\1.8 complex_ruby_script <br/></div>
<p>此时应该可以看到Ruby程序的结果,也许我该说是Java程序的:-) <br/><br/>这就是全部内容了。如果你希望编译Rails项目,然后把它部署到Java EE应用服务器中,请查阅GoldSpike和Warbler以获得更多信息。 </p>

相关推荐

Global site tag (gtag.js) - Google Analytics