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)
aku=5, tootja=Ordi, mudel=Enduro i576CSH Power, lisainfo=, järjekorraNumber=2, raskusaste=5, kiirtellimus=false. Without knowing more about yourArvuticlass there’s no chance of finding the problem.Arvuticlass is missing theprivate static final long serialVersionUID = 1L;. But that would provoke ajava.io.InvalidClassExceptionrather than aClassNotFoundException. That’s the only issue I see. Can you post the exact exception stack trace?