0

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?

Kjir
  • 4,437
  • 4
  • 29
  • 34

1 Answers1

0

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 }
kares
  • 7,076
  • 1
  • 28
  • 38
  • I already have the java bytecode, the problem is packaging it in a way that is usable inside a non-JRuby war... – Kjir Jun 01 '12 at 12:08
  • I'm sorry, you should have refined your question as it's no way clear to me, you mentioned: "I am unable to use the class in the Java code" I think with your comment it's a bit "complicated" at this point to answer what you're looking for. How to boot an embedded jruby container, correctly set it up to load scripts from wherever you package them and then generate bytecode, make sure the web application's class loaders sees the generated classes ? – kares Jun 01 '12 at 12:41
  • I also mentioned that I created a jar with warbler: "I tried creating a jar with marbler, but it places the class in a directory with the project name in front, messing with the expected package structure" But I will try and clarify. – Kjir Jun 01 '12 at 12:45
  • sounds like you could just pre-compile the *.rb files using `jrubyc --javac` (warbler can do that for you I guess). – kares Jun 01 '12 at 12:51
  • I did that - the question is if the only way I have to create the JAR is manually (using `jar cvf ...`) or if there is some other way: warbler creates just executable jars which apparently are of no use for inclusion in a web project... – Kjir Jun 01 '12 at 12:53
  • i see, you can copy the compiled .class files to get them on the web application class-path by copying then into *WEB-INF/classes*. can't tell you how to adjust `warble` to do that but you can always wrap the `warble` command with another executable or copy the files (following the java package structure) directly into the *rails-app.war* is that what you're looking for ? – kares Jun 01 '12 at 13:04
  • Basically yes - but hoping for a more automated way of doing this. You seem to suggest there is none... Thank you, this is the final answer, I'm afraid! – Kjir Jun 01 '12 at 13:06