简体   繁体   English

使用Jruby在Java上调用Ruby类

[英]Call Ruby class over java with jruby

If I implement a class in ruby and compile it with jrubyc than it is not possible to call it from a java class directly if I start it with java. 如果我在ruby中实现一个类并使用jrubyc进行编译,那么如果我使用java启动它,则无法直接从java类中调用它。 If I see this right I have to use org.jruby.embed... to implement a wrapper wich takes a class name and a metod to call my ruby class. 如果我没看错,我必须使用org.jruby.embed...来实现一个包装,该包装使用一个类名和一个方法来调用我的ruby类。

Do I have to do this also if I start the application with jruby? 如果我使用jruby启动应用程序,我也必须这样做吗? In my current project I start java workflow engine completely with jruby. 在我当前的项目中,我完全使用jruby启动Java工作流引擎。 The workflow has to call a method in a ruby class which he cant find. 工作流必须在他找不到的ruby类中调用一个方法。

Maybe easier to understand: 也许更容易理解:

      [ruby_class]   <-----has to call----.
                                          |
jruby [ruby_start_script] --starts--> [java wfe]

I don't know if i understood, but to require your code, you just have to require it and make it sure that it its in your JAVA PATH or APP PATH. 我不知道我是否理解,但是要要求您提供代码,您只需要求它并确保它位于您的JAVA PATH或APP PATH中。 For example, in your jruby script you can do it: 例如,在您的jruby脚本中,您可以执行以下操作:

require 'java'
java_import java.lang.System
version = System.getProperties["java.runtime.version"]

You can call as well java methods like: 您还可以调用以下Java方法:

 java.lang.System.currentTimeMillis

I would say that you make your class work with a java app, then you should put it in your app path, do a require 'java' and java_import your.class.here 我要说的是,让您的类与Java应用程序一起工作,然后应将其放在应用程序路径中,并执行require'java'和java_import your.class.here

If I implement a class in ruby and compile it with jrubyc than it is not possible to call it from a java class directly if I start it with java. 如果我在ruby中实现一个类并使用jrubyc进行编译,那么如果我使用java启动它,则无法直接从java类中调用它。

Not sure if I understand the question entirely but if you are asking if you can call a Jruby method from a Java class using jrubyc, it is possible. 不知道我是否完全理解这个问题,但是如果您要问是否可以使用jrubyc从Java类中调用Jruby方法,则有可能。 Take a look at this blog post which shows how to call a JRuby method from Java . 看一下这篇博客文章,其中展示了如何从Java调用JRuby方法

IIRC, this is one of the newest integrations to JRuby, and may be subject to change. IIRC,这是JRuby的最新集成之一,并且可能会发生变化。 It seems to be possible as described in this post , albeit warns not being very efficient. 尽管警告效率不是很高,但似乎可以按照本文中的描述进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM