1
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Manager {

    public static void main(String[] args) throws Exception {
        serialize();
        deSerialize();
    }

    public static void serialize() throws Exception {

        E obj = new E();
        obj.num = 100;

        F f1 = new F();
        f1.j = 40;
        f1.e1 = obj;

        FileOutputStream fout = new FileOutputStream("test1.txt");
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(f1);
    }

    public static void deSerialize() throws Exception {

        FileInputStream fin = new FileInputStream("test1.txt");
        ObjectInputStream in = new ObjectInputStream(fin);
        F f2 = (F) in.readObject();

        System.out.println(f2.e1.num);
        System.out.println(f2.j);
    }
}

class E implements Serializable {

    int num;
}

class F implements Serializable {
    E e1;
    int j;
}

// Why am I getting java.lang.NoSuchFieldError : numruntime error in this Program.

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Aamir
  • 2,380
  • 3
  • 24
  • 54
  • 1
    Looks like `num` field was not in `E` class when it was serialized into the file. – Luiggi Mendoza May 06 '14 at 21:03
  • Your example works fine on OpenJDK-7. – Benjamin May 06 '14 at 21:28
  • @Benjamin , I am using JDK-7 with eclipse – Aamir May 06 '14 at 21:30
  • Here's my guess: You saved old version of E class into the file (file already consists of some objects) and the one you are trying to read is an old object (without num yet). Could you erase the file and do it again? – Benjamin May 06 '14 at 21:31
  • Yeah I just did, it ran fine, thanks – Aamir May 06 '14 at 21:41
  • 2
    Please, read about serialVersionUID: http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it It would be better if you had this in each serializable – Benjamin May 06 '14 at 21:51
  • @Benjamin If it was a serialVersionUID problem he would have got a different exception showing local and stream serialVersionUIDs. I'm finding it difficult to believe in this question. If the class E really was as stated he *should* have got that exception, and if it had a serialVersionUID he shouldn't have got any exception at all. OP should note that serialized data is binary and the resulting files are not text files, and shouldn't be so named. – user207421 May 06 '14 at 22:42
  • @LuiggiMendoza Adding and deleting fields works in serialization. It looks more like the field 'num' wasn't in the class E when the program was run. I bet a stack trace would show it happened at a line that references 'num' directly, not inside the object steam code. – user207421 May 06 '14 at 22:46
  • @EJP: and serialversionuid exception would have pointed straight to the solution. Look at my guess, it occured to be correct and it would be straightforward if he had serialversionuid. – Benjamin May 07 '14 at 00:26

1 Answers1

0

The only explanation I can think of is that you compiled all the code as shown, then removed 'num' from E and recompiled just that class, then executed the program. All the other scenarios that have been mentioned would cause different exceptions.

A complete stack trace would be interesting. Please provide. Edit it into your question.

user207421
  • 305,947
  • 44
  • 307
  • 483