简体   繁体   English

使用readObject()方法的ClassNotFoundException

[英]ClassNotFoundException with readObject() method

I'm trying to read objects from a file in the Internet. 我正在尝试从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. 现在,当我尝试读取对象(Sulearvuti)时,出现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": 这也是超类“ 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: 线程“主”中的异常java.lang.Error:未解决的编译问题:

Unhandled exception type ClassNotFoundException

at Peaklass.main(Peaklass.java:36)

You are missing some classes contained in the .dat file. 您缺少.dat文件中包含的某些类。 Lookout for the classname shown in the classnotfound exception. 查找在classnotfound异常中显示的类名。

It is not sufficient to have the "Sulearvuti", you also need "Arvuti" (superclass) and "ValeRaskusAsteErind" (Exception) in your classpath. 仅具有“ Sulearvuti”是不够的,您还需要在类路径中使用“ Arvuti”(超类)和“ ValeRaskusAsteErind”(异常)。

BTW the language looks very funny to me, what language is this ? 顺便说一句,语言对我来说看起来很有趣,这是什么语言?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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