« Return to Thread: Serializing Ruby objects from Java

Serializing Ruby objects from Java

by Peter Voss :: Rate this Message:

Reply to Author | View in Thread

Hi,

I am wondering how I can serialize a class written in Ruby from Java.

The Java codebase that I am working on expects some classes to be  
serializable to be able to execute the custom operations that they  
provide on other JVMs. I wanted to provide a Ruby integration that  
allows to implement some of those classes in Ruby.

A minimal example would be something like this:

include_class "java.io.Serializable"
include_class "java.lang.Runnable"

class MyRunnable
    include Serializable, Runnable

    def initialize()
        @num = 0
    end

    def run()
        @num += 1
        puts "Called #{@num} times"
    end
end

MyRunnable.new

In this case the Java code expects this class to implement the  
Runnable interface. I have also made it implement Serializable in the  
hope that this helps making it serializable.

When I get the MyRunnable instance from Java using something like:
Runnable runnable = (Runnable) rubyEngine.eval(script);

I can't write this object to an ObjectOutputStream. If I do this, I  
get this Exception:
Exception in thread "main" java.io.NotSerializableException:  
org.jruby.RubyClass$VariableAccessor
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:
1156)
        at  
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:
1509)
        at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:
1474)
        at  
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:
1392)
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:
1150)

I am using JRuby 1.3.0. Is there a way to serialize objects written in  
Ruby from Java? Would I be able to deserialize these objects on a  
different virtual machine? I am wondering how this would get attached  
to a JRuby runtime.

Any ideas?

Thanks,
--Peter

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Serializing Ruby objects from Java