-1

I am working on windows application in java:

I just test a button that make function login in my system:

My button action performed code:

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);
} 

The function login code:

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);
        }
    }
}
}

The account class code:

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;
}
}

I have a problem: i receive error:

java.lang.classnotfoundexcetpion:Account

and after searching for error reason i found that serialization is the problem of throwing this error because i test this code before in another function that dont use serialization and its worked perfectly.

so my question is: how to fix this error?

NOTE: my application is not a client-server application ... so there is no two projects created ... just only one.

MHM
  • 19
  • 10
  • Are those 2 different applications? and does the other app have the class `Account` ? – vlatkozelka Dec 05 '15 at 20:19
  • Is this a client server application? If that is the case then notice that the package name **must** be equal on both the server and the client. – Cyclonecode Dec 05 '15 at 20:19
  • 2
    http://stackoverflow.com/questions/2916107/readobject-method-throws-classnotfoundexception – Cyclonecode Dec 05 '15 at 20:21
  • not client-server application.... its java se window application .... class emp and class account are in the same package – MHM Dec 05 '15 at 20:22
  • i read the answers of the link posted but really iam not fully understand what to do ..... @Cyclone .... and its not a client-server application. can you please add more details of what to do ?? – MHM Dec 05 '15 at 20:47
  • @Cyclone i am using netbeans also but its not 2 different projects ... its one only – MHM Dec 05 '15 at 20:51
  • Where does the `employees.bin` file comes from? Is it created from the same application? – Cyclonecode Dec 05 '15 at 21:31
  • 1
    @Cyclone After search i found what is the problem ... you was alright sir .... class `Account` was not have the static member `private static final long serialVersionUID = 1L;` ... so i serialize the 'employees.bin' again after adding the static member ... this static member is used for locating class `Account `after it has been used in serialization... it is not written before in the code so it throws error ... `classnotfoundexception` but now when deserialize object it used to locate the class and code now is running good ... thanks you so much sir ... please write answer again to accept. – MHM Dec 05 '15 at 21:53
  • It isn't used for locating anything. It is used to verify compatibility of the data in the stream with the class that is present when deserializing. Leaving it out does **not** cause `ClassNotFoundException`: it *may* cause `InvalidClassException`. Either you're barking up the wrong tree completely, or you're misreporting the symptoms of your problem. – user207421 Dec 05 '15 at 22:36

1 Answers1

1

long discussions about this:

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 advices :

  • be sure to put the private static final long serialVersionUID = XXX;

  • be sure to embark your class in your classpath/jar

  • force it in code with Account ac=new Account(); // See if problem here

it helps ?

Community
  • 1
  • 1