How to deploy a Ruby class in a Java web project

179 Views Asked by At

I am trying to create an hybrid between a JRuby and a Java application, so that it may be possible to migrate single components at a time. What I am trying to do right now is really simple, here is the Ruby class: require 'java'

java_package 'eu.netprophecy.test'

class RubyViewModel
  java_signature 'String getGreeting()'
  def getGreeting
    puts "Hello, ZK, from Ruby!"
  end
end

I want to be able to use this View Model from a ZK project:

<?page title="Hello, Ruby!" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Hello Ruby!!" border="normal" width="200px"     apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('eu.netprophecy.test.RubyViewModel')">

<label value="@load(vm.greeting)"/>

</window>
</zk>

I tried creating a jar with marbler, but it places the .class file with the java bytecode in a directory with the project name in front, messing with the expected package structure (in this case I get EJB/eu/netprophecy/test/RubyViewModel, which means I am unable to use the generated Java class in the Java code).

Does anyone know if this is at all possible, if there are better ways to do it,, or if I have to create manually the jar?

1

There are 1 best solutions below

7
On

you need to generate a java class from your ruby class which is perfectly possible (even without pre-compilation) with become_java!, just make sure the ruby script gets executed before the view is rendered (or compiled I'm not sure what you're using).

class RubyViewModel
  java_signature 'String getGreeting()'
  def getGreeting
    puts "Hello, ZK, from Ruby!"
  end
end

require 'jruby/core_ext'
RubyViewModel.become_java! # returns a java.lang.Class

you might check the method is there (the java way) using :

RubyViewModel.java_class.getDeclaredMethods.map { |method| method.to_s }