This code...
class A implements Serializable{
String str
int n
}
try{
def a= new A(str:'abc', n:7)
def out= new ObjectOutputStream(new FileOutputStream('serializedObject.obj'))
out.writeObject(a)
out.close()
}finally{}
try{
def inp= new ObjectInputStream(new FileInputStream('serializedObject.obj'))
def a2= inp.readObject()
inp.close()
}finally{}
...produces error...
java.lang.ClassNotFoundException: A
at java_io_ObjectInput$readObject.call(Unknown Source)
at otherRun.run(otherRun.groovy:16)
...when attempting to reload the object in the 2nd try block. It works OK when the class is a predefined class such as java.util.List. The above code also works OK when converted line-for-line into Java.
How can I get it working in Groovy?
Put your "class A" into its own file and make sure the "A.class" file is availbale (in classpath) where you read the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With