How to call Java API from Ruby 1.8 or 1.9

前端 未结 1 1896
春和景丽
春和景丽 2020-12-21 07:38

I\'ve this JRuby working code (stolen from Keith\'s Blog), which wraps the SAXON xslt processor API.

Now, I won

相关标签:
1条回答
  • 2020-12-21 08:13

    As commented above, you can not do this directly from the Ruby runtime, calling Java from Ruby requires you to be either on JRuby or call Java indirectly using the C/C++ JVM API that allows you to call Java code from C.

    The first option is possibly using Ruby Java Bridge that does most of the heavy lifting for you (it functions as a Ruby-to-C-to-Java wrapper).

    If RJB doesn't work for you, you can also build your wrapper directly by using the JVM API in C ( example here ) and then you could call it from Ruby using FFI.

    But unless you really need to use the C-Ruby (MRI) I would greatly recommend you to avoid any of the approaches above and just use JRuby, as delving into native code will lead to possible segfaults, memory management issues and all options above force you to run in a single thread, while you could build a multi-threaded solution by using JRuby.

    0 讨论(0)
提交回复
热议问题