我正在使用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
并且在搜索错误原因之后我发现序列化是抛出此错误的问题,因为我之前在另一个不使用序列化的函数中测试此代码并且它完美地工作。
所以我的问题是:如何修复此错误?
注意:我的应用程序不是客户端 - 服务器应用程序...所以没有创建两个项目......只有一个。
答案 0 :(得分:1)
长期讨论这个问题:
ClassNotFoundException when deserializing a binary class file's contents
ClassNotFoundException during Deserialization of a just-serializaed class
Java SerialIzation: 'ClassNotFoundException' when deserializing an Object
3条建议:
请务必将私有静态最终版本的长版本序列为VersionUID = XXX;
务必将您的课程放在classpath / jar
使用Account ac = new Account()强制进入代码; //看看这里是否有问题
有帮助吗?