java.lang.ClassNotFoundException怎么解决啊,请哪位高手帮帮忙!

david_16885678 2011-03-19 01:45:16
package rexlh;
import java.lang.Exception;
import java.io.*;
import java.io.IOException;
import java.io.Serializable;
public class ReadTeacher {

/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(new FileInputStream("teacher.txt"));
Teacher t1=(Teacher)ois.readObject();
Teacher t2=(Teacher)ois.readObject();
Person p=(Person)ois.readObject();
Teacher t3=(Teacher)ois.readObject();
System.out.println("t1:"+(t1.getStudent()==p));
System.out.println("t2:"+(t2.getStudent()==p));
System.out.println("t2与t3"+(t2==t3));
}
catch(IOException ex)
{ ex.printStackTrace();
}
finally{
try{
if(ois!=null)
ois.close();}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
...全文
304 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ITJava 2011-03-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 canghaiguzhou 的回复:]
建议把异常信息贴出来,到底是哪一个类,找不到
[/Quote]
如果Teacher和Person类没有的话就按上面4楼的做,要是保证有的话就要把你的具体的一场信息贴出来,按照四楼的应该是能解决的
zx8813443 2011-03-20
  • 打赏
  • 举报
回复
建议把异常信息贴出来,到底是哪一个类,找不到
  • 打赏
  • 举报
回复
java.lang.ClassNotFoundException 找不到类,仔细检查下,刚刚看了好多代码问题了,脑袋有点晕,就不看了。。。。
david_16885678 2011-03-20
  • 打赏
  • 举报
回复
我的Teacher和Person类(怕代码太多没贴出来)是定义在 package rexlh 这个包里,但是也抛出这个异常啊?
uyu2yiyi 2011-03-19
  • 打赏
  • 举报
回复
你的Teacher和Person类是不是定义在 package rexlh 这个包里?

如果不是的话,在这个包里定义这2个类就能找到了。


也可以像1楼这样定义在ReadTeacher类后面,都能解决
xiaona1047985204 2011-03-19
  • 打赏
  • 举报
回复
没有找到teacher 与person类,你没有实现这两个类
bigTail_Wolf_ 2011-03-19
  • 打赏
  • 举报
回复
找不到类
teacher.txt 这里面的内容对吗 ?
Person哪里来的 ?
runer 2011-03-19
  • 打赏
  • 举报
回复
你这个程序里面用到了Teacher类和Person类

但代码里面没有这两个类的定义。

下面是一个可运行的版本,但是没有功能

import java.lang.Exception;
import java.io.*;
public class ReadTeacher {

/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("teacher.txt"));
Teacher t1;
try {
t1 = (Teacher) ois.readObject();
Teacher t2 = (Teacher) ois.readObject();
Person p = (Person) ois.readObject();
Teacher t3 = (Teacher) ois.readObject();
System.out.println("t1:" + (t1.getStudent() == p));
System.out.println("t2:" + (t2.getStudent() == p));
System.out.println("t2与t3" + (t2 == t3));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

class Person{}
class Teacher extends Person{

public Person getStudent() {
return null;
}}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧