私はあなたの問題を解決することができました。実例をお見せしましょう
私が使用するいくつかのEmployeeクラス
public class Employee implements java.io.Serializable
{
public String name;
public String address;
}
それからMainクラス
class Main {
public static void main(String[] args) {
Employee e1 = new Employee()
e1.name = 'John'
e1.address = 'Main Street'
byte[] bytes = []
ByteArrayOutputStream stream = new ByteArrayOutputStream()
ObjectOutputStream out = new ObjectOutputStream(stream)
out.writeObject(e1)
bytes = stream.toByteArray()
out.close()
stream.close()
Object o = null
new ByteArrayInputStream(bytes).withObjectInputStream(Main.getClassLoader()){ gin ->
o = gin.readObject()
}
print o instanceof Employee
println 'Deserialized Employee...'
println 'Name: ' + o.name
println 'Address: ' + o.address
}
}
getClass().classLoaderを投げていた をする代わりにjava.lang.ClassNotFoundException、私は をやっていMain.getClassLoader()ます。このクラスローダーは私のEmployeeクラスを見つけることができます。
さらに、実際にオブジェクトをキャストする必要はありません。オブジェクトはグルーヴィーで動的であるため、実行時にフィールドnameとaddressフィールドを取得します。
ただし、いつでもオブジェクトのタイプを確認してからキャストできます。
print o instanceof Employee
これは戻りますtrue