读书人

显示找不到类的错误求大神们解决啊

发布时间: 2013-08-01 15:23:18 作者: rapoo

显示找不到类的异常,求大神们解决啊!
//LoginInterface.java
package com.im.client.view;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import com.im.client.common.*;
import com.im.client.model.*;

public class LoginInterface extends JFrame implements ActionListener{

//定义组件
JPanel jp1;
JPanel jp2;
JPanel jp3;

JLabel jb1 = null;
JTextField userName = null;

JLabel jb2 = null;
JPasswordField password = null;

JButton login = null;
JButton register = null;

//构造函数
public LoginInterface(){
//创建组件
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();

jb1 = new JLabel("IM号码",JLabel.CENTER);
userName = new JTextField(20);

jb2 = new JLabel("密 码",JLabel.CENTER);
password = new JPasswordField(20);

login = new JButton("登录");
register = new JButton("注册");

//设置组件属性
jb1.setFont(new Font("宋体",Font.PLAIN,14));
jb2.setFont(new Font("宋体",Font.PLAIN,14));
userName.setFont(new Font("宋体",Font.PLAIN,18));
password.setFont(new Font("宋体",Font.PLAIN,18));
login.setFont(new Font(null,Font.PLAIN,14));
register.setFont(new Font(null,Font.PLAIN,14));
login.setBackground(Color.white);
register.setBackground(Color.white);

//组件事件
login.addActionListener(this);

//添加组件
jp1.add(jb1);
jp1.add(userName);

jp2.add(jb2);
jp2.add(password);

jp3.add(login);
jp3.add(register);

this.add(jp1);
this.add(jp2);
this.add(jp3);

//设置窗口属性
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)screensize.getWidth();
int height = (int)screensize.getHeight();

this.setIconImage((new ImageIcon("images/icon.gif")).getImage());
this.setTitle("IM登陆");
this.setSize(260,200);
this.setLocation(width/2-130,height/2-200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(new GridLayout(3,1));

//显示窗口
this.setVisible(true);
}

public static void main(String[] args){
LoginInterface l = new LoginInterface();
}

public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == login){


CheckUser cku = new CheckUser();

User u = new User();
u.setUserName(userName.getText());
u.setPassword(new String(password.getPassword()));

if(cku.checkLogin(u)){
new FriendList();
this.dispose();
}
else{
JOptionPane.showMessageDialog(this,"用户名密码错误!");
}
}
}
}

//CheckUser.java
package com.im.client.model;

import com.im.client.common.*;

public class CheckUser{

public boolean checkLogin(User u){
System.out.println("从登陆界面接受的信息:"+u.getUserName()+","+u.getPassword());
return new ConnectServer().sendLoginInfoToServer(u);
}
}


//ConnectServer.java
package com.im.client.model;

import java.net.*;
import java.io.*;
import com.im.client.common.*;

public class ConnectServer{

public boolean sendLoginInfoToServer(Object o){
boolean b = false;
try{
Socket s = new Socket("localhost",7878);

ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(o);
User u = (User)o;
System.out.println("从CheckUser接受到的信息:"+u.getUserName()+","+u.getPassword());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
Message m = (Message)ois.readObject();

if(m.getMessageId() == "0"){
b = true;
}
else{
b = false;
}
}
catch(Exception e){
e.printStackTrace();
}

return b;
}
}

//然后这里接受不到,显示异常,求解决啊!!提示是
//ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
//User u = (User)ois.readObject();
//出问题。。
显示找不到类的错误,求大神们解决啊

package com.im.server.model;

import java.net.*;
import java.io.*;
import com.im.server.common.*;

public class ImServer{

public ImServer(){
try{
ServerSocket ss = new ServerSocket(7878);
System.out.println("服务器已开启");

while(true){
Socket s = ss.accept();
System.out.println("客服端连接成功");

ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
User u = (User)ois.readObject();
System.out.println("从客服端接受到的信息:"+u.getUserName()+","+u.getPassword());

Message m = new Message();
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());


if(u.getPassword().equals("123456")){
m.setMessageId("0");
oos.writeObject(m);
}
else{
m.setMessageId("1");
oos.writeObject(m);
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
[解决办法]

引用:
Quote: 引用:

难道我说的不明吧,你client项目里和server项目里的User这个类的包路径不一致

server 和 client 分成两个项目写的啊
client的User和server的User是两个文件 但内容一样不行?

你仔细看一下我4L说的啊,你在imServer中从对象流中读出来这个对象User,这个是一个序列化的对象,所以JVM对其进行反序列化,反序列化会从中读出完整的类路径(包名+类名),你这个例子中是com.im.client.common.User,然后JVM试图在你的imServer项目中通过classpath加载这个类,但是你imServer没有com.im.client.common.User(你的是com.im.server.common.User),所以报classnotfound啊,我让你把你imServer中的User和message这两个类的包结构改成com.im.client.common.User的啊

读书人网 >J2SE开发

热点推荐