【发布时间】:2015-12-05 20:14:07
【问题描述】:
我正在用java开发windows应用程序:
我只是在我的系统中测试了一个使功能登录的按钮:
我的按钮动作执行代码:
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
if(emp.isSelected()) // get the selected radio button
{
Account a = new Account();
Emp e = new Emp();
a.setUsername(username.getText().toUpperCase());
a.setPassword(password.getText().toUpperCase());
e.login(a);
this.dispose();
}
else if(supp.isSelected())
{
}
else if(admin.isSelected())
{
Account a = new Account();
Admin m = new Admin();
a.setUsername(username.getText().toUpperCase());
a.setPassword(password.getText().toUpperCase());
m.login(a);
this.dispose();
}
else
JOptionPane.showMessageDialog(null, "Please select a choice", "Alert", JOptionPane.INFORMATION_MESSAGE);
}
函数登录代码:
public class Emp
{
public void login(Account a)
{
boolean find = false;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream("C:\\Users\\فاطمة\\Downloads\\employees.bin"));
ArrayList<Account> b = (ArrayList)in.readObject();
Iterator<Account> i = b.iterator();
while(i.hasNext())
{
Account ac = i.next();
if(ac.getUsername().equals(a.getUsername()) && ac.getPassword().equals(a.getPassword()))
{
find = true;
}
else
JOptionPane.showMessageDialog(null, "Wrong username or password .. try again !!", "Login Failed",JOptionPane.ERROR_MESSAGE);
}
if(find)
{
JOptionPane.showMessageDialog(null, "Welcome " + a.getUsername(), "Login Success", JOptionPane.INFORMATION_MESSAGE);
emp_page e = new emp_page();
e.setLocation(350, 150);
e.setSize(400, 490);
e.setTitle("Products Management");
e.setVisible(true);
}
} catch (FileNotFoundException ex) {
//Logger.getLogger(Emp.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ClassNotFoundException ex) {
//Logger.getLogger(Emp.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
in.close();
} catch (IOException ex) {
//Logger.getLogger(Emp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
账户类代码:
import java.io.Serializable;
public class Account implements Serializable{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我有一个问题:我收到错误:
java.lang.classnotfoundexcetpion:Account
在搜索错误原因后,我发现序列化是引发此错误的问题,因为我之前在另一个不使用序列化的函数中测试了这段代码,并且它运行良好。
所以我的问题是:如何解决这个错误?
注意:我的应用程序不是客户端-服务器应用程序...所以没有创建两个项目...只有一个。
【问题讨论】:
-
这两个是不同的应用程序吗?另一个应用程序有
Account类吗? -
这是一个客户端服务器应用程序吗?如果是这种情况,请注意包名称必须在服务器和客户端上都相同。
-
不是客户端-服务器应用程序......它的java se窗口应用程序......类emp和类帐户在同一个包中
-
@Cyclone 搜索后我发现问题出在哪里......你没事先生......类
Account没有静态成员private static final long serialVersionUID = 1L;......所以我序列化' employees.bin' 添加静态成员后再次...这个静态成员用于定位类Account在它被用于序列化之后...它之前没有写在代码中所以它会抛出错误...@ 987654330@ 但是现在当反序列化对象时,它用于定位类和代码现在运行良好......非常感谢先生......请再次写答案以接受。
标签: java serialization deserialization