简体   繁体   中英

ClassNotFoundException with readObject() method

I'm trying to read objects from a file in the Internet. I have been given the object class, which is this:

import java.io.Serializable;


public class Sulearvuti extends Arvuti implements Serializable {

    private static final long serialVersionUID = 1L;

    //isendiväli
    private int aku;


    //konstruktor
    public Sulearvuti(String tootja, String mudel, String lisainfo,
            int järjekorraNumber, int raskusaste, boolean kiirtellimus, int aku)
            throws ValeRaskusAsteErind {
        super(tootja, mudel, lisainfo, järjekorraNumber, raskusaste,
                kiirtellimus);
        this.aku = aku;
    }

    // meetod toString, kasutama ülemklassi meetodit
    public String toString() {
        return "Sülearvuti [aku=" + aku + ", " + super.toString() + "]";
    }

    // meetodi ülekatmine
    double parandamiseAeg(){
        return this.getRaskusaste()*2;
    }


}

Now when I'm trying to read the objects (Sulearvuti), I get ClassNotFoundException. This is the piece of code :

ObjectInputStream ois = 
    new ObjectInputStream (
        new URL("http://www.ut.ee/~marinai/sulearvutid.dat")
        .openConnection()
        .getInputStream());

int arv=ois.readInt();
Sulearvuti sülearvuti=(Sulearvuti)ois.readObject();

There's no problem with the Integer, but it won't recognize the class. I've been desperate for the past hour or so...

Also here's the code for the superclass "Arvuti":

import java.io.Serializable;

public class Arvuti implements  Serializable, Comparable<Arvuti> {

private String tootja;
private String mudel;
private String lisainfo;
private int jrnumber;
private int vea_raskusaste;
private boolean kiirtellimus;
String getTootja() {
    return tootja;
}
String getMudel() {
    return mudel;
}
String getLisainfo() {
    return lisainfo;
}
int getJrnumber() {
    return jrnumber;
}
int getVea_raskusaste() {
    return vea_raskusaste;
}
boolean isKiirtellimus() {
    return kiirtellimus;
}
void setTootja(String tootja) {
    this.tootja = tootja;
}
void setMudel(String mudel) {
    this.mudel = mudel;
}
void setLisainfo(String lisainfo)throws WindowsXPErind {
    this.lisainfo = lisainfo;
    if(lisainfo.contains("WindowsXP"))throw new WindowsXPErind();

}
void setJrnumber(int jrnumber) {
    this.jrnumber = jrnumber;
}
void setVea_raskusaste(int vea_raskusaste)throws ValeRaskusAsteErind {
    if(vea_raskusaste<1 || vea_raskusaste>10) throw new ValeRaskusAsteErind();
    this.vea_raskusaste = vea_raskusaste;
}
void setKiirtellimus(boolean kiirtellimus) {
    this.kiirtellimus = kiirtellimus;
}
Arvuti(String tootja, String mudel, String lisainfo, int jrnumber,
        int vea_raskusaste, boolean kiirtellimus)throws ValeRaskusAsteErind {
    try{
    setTootja( tootja);
    setMudel(mudel);    
    setJrnumber(jrnumber);
    setVea_raskusaste(vea_raskusaste);
    setKiirtellimus(kiirtellimus);
    setLisainfo(lisainfo);
    }
    catch (WindowsXPErind e){
        System.out.println("WindowsXPErind");
        setVea_raskusaste(vea_raskusaste+2);
    }
}
double parandamiseAeg(){
    return getVea_raskusaste()*1.5;
}
public String toString() {
    return "Arvuti [tootja=" + tootja + ", mudel=" + mudel + ", lisainfo="
            + lisainfo + ", järjekorranumber=" + jrnumber + ", vea raskusaste="
            + vea_raskusaste + ", kiirtellimus=" + kiirtellimus
            + ", parandamise aeg=" + parandamiseAeg() + "]";
}
public int compareTo(Arvuti arvuti){
    if(this.isKiirtellimus()==true && arvuti.isKiirtellimus()==false) return -1;
    else if(this.isKiirtellimus()==false && arvuti.isKiirtellimus()==true) return 1;
    else{
        if(this.getJrnumber()<arvuti.getJrnumber())return -1;
        else if(this.getJrnumber()>arvuti.getJrnumber())return 1;
        else return 0;
    }
}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Unhandled exception type ClassNotFoundException

at Peaklass.main(Peaklass.java:36)

You are missing some classes contained in the .dat file. Lookout for the classname shown in the classnotfound exception.

It is not sufficient to have the "Sulearvuti", you also need "Arvuti" (superclass) and "ValeRaskusAsteErind" (Exception) in your classpath.

BTW the language looks very funny to me, what language is this ?

应用程序的类路径上的“ Sulearvuti”类是否试图反序列化对象?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM