How to Deserialize Java Serialized object in C#?

I want to deserialize the java Serialized object. For this I write the following code:

// Main Method

[STAThread]static void Main() {

try{Sample p = new Sample();

Stream s = System.IO.File.OpenRead(@"C:\test4.bin");

BinaryFormatter b = new BinaryFormatter();

p = (Sample)b.Deserialize(s);

Console.WriteLine("I:" + p.name);

Console.WriteLine("I:" + p.age); }

catch(Exception ex){MessageBox.Show(ex.Message);}

Application.Run(new Form1()); } }

//Class to typecast object after deserializing...

[Serializable()]

public class Sample : ISerializable{

public string name;

public int age;

public Sample(){

name = null; age = 0; }

public Sample(SerializationInfo info, StreamingContext ctxt) {

//Get the values from info and assign them to the appropriate properties

age = (int)info.GetValue("age", typeof(int));

name = (String)info.GetValue("name", typeof(string)); }

//Serialization function.

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)

{

info.AddValue("age", age);

info.AddValue("name", name); } }

When I run the code I got the error on the line ->

p = (Sample)b.Deserialize(s); which I have written in the main method. The following error comes

"Binary Formatter Version incompatibility. Expected version 1.0. Received version 1835094830.493186160 " .

I read on the Net that Java Serialized object can be deserialized but I am not able to do so.

Can anybody tell us what I am missing.




Answer this question

How to Deserialize Java Serialized object in C#?

  • Rob Goodridge

    It won't work, there are some companies that are specialized in interopability between Java and .NET like JNBrigde.

    Otherwise you have to write your own serialization in java and deserialization in C#. Xml Serialization shouldn't be that hard to implement if you don't want to fancy stuff.


  • How to Deserialize Java Serialized object in C#?