Java 反序列化对象时classnotfoundexception?
我正在用java开发windows应用程序: 我只是测试了一个按钮,该按钮使函数登录到我的系统中: 我的按钮操作执行代码:Java 反序列化对象时classnotfoundexception?,java,serialization,deserialization,Java,Serialization,Deserialization,我正在用java开发windows应用程序: 我只是测试了一个按钮,该按钮使函数登录到我的系统中: 我的按钮操作执行代码: private void loginActionPerformed(java.awt.event.ActionEvent evt) { if(emp.isSelected()) // get the selected radio button { Account a
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);
}
}
}
}
我有一个问题:我收到错误:
java.lang.classnotfoundexcetpion:Account
在搜索错误原因之后,我发现序列化就是抛出这个错误的问题,因为我以前在另一个不使用序列化的函数中测试过这段代码,并且它工作得很好
所以我的问题是:如何修复这个错误
注意:我的应用程序不是客户机-服务器应用程序。。。所以没有创建两个项目。。。只有一个。关于这一点的详细讨论: 3.建议:
- 确保将私有静态final long serialVersionUID=XXX
- 确保在类路径/jar中开始类
- 使用Account ac=new Account();/,在代码中强制它看看这里是否有问题
有帮助吗?这两种应用程序是否不同?另一个应用程序是否有类
帐户
?这是客户机-服务器应用程序吗?如果是这种情况,请注意服务器和客户端上的包名必须相等。而不是客户端-服务器应用程序。。。。它的JavaSE窗口应用程序。。。。类别emp和类别帐户在同一个帐户中package@Cyclone经过搜索我发现了什么问题。。。你还好吧,先生。。。。classAccount
没有静态成员private static final long serialVersionUID=1L代码>。。。因此,在添加静态成员后,我再次序列化了“employees.bin”。。。此静态成员用于在序列化中使用后定位类帐户
。。。它以前没有在代码中写入,因此会抛出错误classnotfoundexception
但是现在当反序列化对象时,它用来定位类,代码现在运行良好。。。非常感谢你,先生。。。请再写一次答案接受。这勉强算是一个答案。唯一相关的是第二个要点。@EJP所有相关的讨论都没有给出肯定的答案,只列出了症状、可能的原因和应对措施,我将继续讨论。如果存在一个确定的解决方案,那将是一个很大的进步,不是吗?
java.lang.classnotfoundexcetpion:Account