0

I have a class that implements serializable

package lligafutbolserial.aaron.gomez;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Scanner;

public class Equip implements Comparable<Equip>, Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;
transient Integer puntsLliga;
transient String nomEquip;
HashMap<Integer, Jugador> jugadors;
transient Scanner in;

public Equip(String nom) {
    this.nomEquip = nom;
    this.puntsLliga = 0;
    this.jugadors = new HashMap();
}

public HashMap<Integer, Jugador> getJugadors() {
    return jugadors;
}

public void setJugadors(HashMap<Integer, Jugador> jugadors) {
    this.jugadors = jugadors;
}

public String getNom() {
    return nomEquip;
}

public int getPunts() {
    return puntsLliga;
}

public void incrementaPunts(int punts) {
    puntsLliga += punts;
}

@Override
public String toString() {
    return "Equip: puntsLliga=" + puntsLliga + ", nomEquip=" + nomEquip;
}

@Override
public int compareTo(Equip o) {
    // TODO Auto-generated method stub
    return o.puntsLliga.compareTo(this.puntsLliga);
}

public HashMap<Integer, Jugador> afegirJugadors() {
    in = new Scanner(System.in);
    do {
        if (jugadors.size() > 0) {
            in.nextLine();
        }
        Jugador jugador = new Jugador();
        System.out.println("Nom del jugador: ");
        jugador.setNom(in.nextLine());
        System.out.println("Cognom del jugador: ");
        jugador.setCognom(in.nextLine());
        System.out.println("Dorsal del jugador: ");
        jugador.setDorsal(in.nextInt());
        System.out.println("Edat del jugador: ");
        jugador.setEdat(in.nextInt());
        System.out.println("Dona'm l'al�ada del jugador: ");
        jugador.setAlsada(in.nextFloat());
        jugador.setGols(0);
        jugador.setEquip(this);
        jugadors.put(jugador.getDorsal(), jugador);
    } while (jugadors.size() < 15);
    return jugadors;
}

public boolean afegirUnJugador() {
    in = new Scanner(System.in);

    Jugador jugador = new Jugador();

    System.out.println("Nom del jugador: ");
    jugador.setNom(in.nextLine());
    System.out.println("Cognom del jugador: ");
    jugador.setCognom(in.nextLine());
    System.out.println("Dorsal del jugador: ");
    jugador.setDorsal(in.nextInt());
    System.out.println("Edat del jugador: ");
    jugador.setEdat(in.nextInt());
    System.out.println("Dona'm l'al�ada del jugador: ");
    jugador.setAlsada(in.nextFloat());
    jugador.setGols(0);
    jugador.setEquip(this);
    if (jugadors.put(jugador.getDorsal(), jugador) == null) {
        return true;
    } else {
        return false;
    }
}

}

I write to the file with the next method:

public void escriureTot() throws IOException{
    String nomFitxer = "";
    for(Equip e : equips){
        System.out.println(e.getNom());
        nomFitxer = "LligaBBVA/"+ e.getNom();
        writerTot = new ObjectOutputStream(new FileOutputStream(nomFitxer));
        writerTot.writeObject(e);
        writerTot.close();
    }
}

I use this loop to get the filename and read from the file.

        for (Path fitxers : ds) {
        System.out.println(fitxers.toString());
        System.out.println(fitxers.toString().substring(10));
        String nomEquip = fitxers.toString().substring(10);
        ObjectInputStream llegir = new ObjectInputStream(new FileInputStream("LligaBBVA/" + nomEquip));
        equip = (Equip) llegir.readObject();
        equips.add(equip);
        llegir.close();
    }

But when i try equip = (Equip) llegir.readObject(); this error appears:

Exception in thread "main" java.lang.ClassNotFoundException: lligafutbol.aaron.gomez.Equip
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:626)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1613)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at lligafutbolserial.aaron.gomez.Lliga.<init>(Lliga.java:51)
at lligafutbolserial.aaron.gomez.Principal.main(Principal.java:14)

I've tried to be the clearest possible, sorry if its hard to understand. Thx m8's!

7
  • Looks like the project where you're reading the data doesn't have this Equip class defined. Commented Mar 3, 2015 at 15:56
  • are Principal, and Equip classes in same package? If no, you imported Equip? Commented Mar 3, 2015 at 15:58
  • if your lligafutbol.aaron.gomez.Equip class in inside a JAR, make sure to add that the Principal's class-path. How exactly are you invoking your code ? Commented Mar 3, 2015 at 15:59
  • All the classes are in the same package. Commented Mar 3, 2015 at 16:05
  • This one lligafutbolserial.aaron.gomez.Principal and lligafutbol.aaron.gomez.Equip are completely different packages :) Commented Mar 3, 2015 at 16:07

2 Answers 2

1

You appear to have changed package names between writing (at least one of) the files and trying to read them back. You present code for class lligafutbolserial.aaron.gomez.Equip, but the class of the object Java is trying to read is lligafutbol.aaron.gomez.Equip ("lligafutbolserial" vs. "lligafutbol").

Note, too, that you appear to be naming -- and presumably arranging -- your packages in an odd way. Since package names are mapped to a directory tree by the compiler, it is normally most convenient to order the segments from most general to most specific. That is, gomez.aaron.lligafutbolserial. Substantially all Java code is named and arranged that way.

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, thats what i actually made, but how do i change the class Equip that i want to pick? Coz i dont want lligafutbol.aaron.gomez.Equip anymore, i want lligafutbolserial.aaron.gomez.Equip. Thanks a lot!
correct your import statement in Principal class, if you want to use the one in same package, just remove the explicit import for lligafutbol.aaron.gomez.Equip
They actually are all in the same package, but the error will still come out. Im sorry, never happen this before... I mean, i've checked the imports and the package, all at the same package.
The problem is not with your cast, it is that you have serialized an object of a class that is not available when you try to deserialize it. You cannot change an object's class via serialization/deserialization. Java serialization format is primarily for object transmission; it is not well suited for long-term storage. If you don't want to change your package name back to the original, then you'll have to serialize new objects. Make sure to delete all the old files from disk.
Thanks, ill try that and post the result!
|
0

Your code has

package lligafutbolserial.aaron.gomez;
//...
public class Equip

The error message says:

ClassNotFoundException: lligafutbol.aaron.gomez.Equip

Apparently you serialized an object of another class Equip in another package lligafutbol. You must use identical classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.