1

I need to update my project, I used a lowercase on classes and its not standard coding convention. So, I have used the refractor > rename in eclipse for the classes. And now I get this exception/error:

Exception in thread "main" java.lang.NoClassDefFoundError: 
FacebookClass/facebookClass (wrong name: FacebookClass/FacebookClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at FacebookClass.Driver.main(Driver.java:28)

I used the refractor and had the box checked to change all names, I don't know what the problem is.

Driver Class:

package FacebookClass;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class Driver {

public static void main(String args[])
{
    Scanner input = new Scanner(System.in);
    String username;
    String password;
    int choice = 0;

    FacebookClass facebook = new FacebookClass();
    String filename = "save.ser";

    // Deserializing facebook object 
    try
    {    
        FileInputStream file = new FileInputStream(filename); 
        ObjectInputStream in = new ObjectInputStream(file); 

        facebook = (FacebookClass)in.readObject(); 

        in.close(); 
        file.close(); 

        System.out.println("Succefully opened saved data."); 
    } 

    catch(IOException ex) 
    { 

    } 

    catch(ClassNotFoundException ex) 
    { 

    } 

    while (choice != 5)
    {
    choice = menu();

    switch(choice)
    {
    case 1:
        // List users
        System.out.println("");
        facebook.listUsers();
        break;
    case 2:
        // Add a user
        System.out.println("");
        System.out.print("Enter a username: ");
        username = input.nextLine();
        facebook.addUser(username);
        break;
    case 3:
        // Delete a user
        System.out.print("Enter your username: ");
        username = input.nextLine();
        facebook.deleteUser(username);
        break;
    case 4:
        // Get password hint
        System.out.print("Enter your username: ");
        username = input.nextLine();
        facebook.getHint(username);
        break;
    case 5:
        // Quit and serialize facebook object 
        try
        {     
            FileOutputStream file = new FileOutputStream(filename); 
            ObjectOutputStream out = new ObjectOutputStream(file); 

            out.writeObject(facebook); 

            out.close(); 
            file.close(); 

            System.out.println("Data has been saved."); 

        } 

        catch(IOException ex) 
        { 

        } 
    }
}

}
public static int menu()
{
    Scanner input = new Scanner(System.in);
    int choice = 0;
    System.out.println("");
    System.out.println("1) List Users");
    System.out.println("2) Add a User");
    System.out.println("3) Delete a User");
    System.out.println("4) Get Password Hint");
    System.out.println("5) Quit"); 
    System.out.print("Enter your choice: ");
    choice = input.nextInt();
    while (choice < 1 || choice > 5)
    {
        System.out.print("Try again: ");
        choice = input.nextInt();
    }
    return choice;
}

}

FacebookClass Class:

package FacebookClass;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class FacebookClass implements Serializable
{

private ArrayList<FacebookUser> users = new ArrayList<FacebookUser>();

public void listUsers()
{
    // Print user names of users array list
    System.out.println("Users: ");
    for (int i=0; i<users.size(); i++)
    {
        System.out.println((users.get(i)).getUsername());
    }

}

public void addUser(String username)
{
    Scanner input = new Scanner(System.in);
    String password;
    String hint;
    // Add a user, check if name exists
    Boolean nameCheck = false;
    for (int i=0; i<users.size(); i++)
    {
        if (users.get(i).getUsername().equals(username))
        {
            nameCheck = true;
        }
    }
    if (nameCheck)
    {
        System.out.println("Error! Username is already in 
use.");

    }
    else
    {
        System.out.print("Enter a password: ");
        password = input.nextLine();
        System.out.print("Enter a password hint: ");
        hint = input.nextLine();
        FacebookUser user = new FacebookUser(username, 
password);
        user.setPasswordHint(hint);
        users.add(user);
    }

}

public void deleteUser(String username)
{
    Scanner input = new Scanner(System.in);
    String passwordTemp;
    Boolean usernameCheck = false;
    Boolean passwordCheck = false;
    for (int i=0; i<users.size(); i++)
    {
        if ((users.get(i)).getUsername().equals(username))
        {
            System.out.print("Enter your password to 
confirm: ");
            passwordTemp = input.nextLine();
            usernameCheck = true;
            if 
(!passwordTemp.equals(users.get(i).getPassword()))
            {
                System.out.println("Error! Passwords do 
not match.");
                usernameCheck = false;
                passwordCheck = true;
            }
            if (!passwordCheck)
            {
                users.remove(i);
            }

        }
    }
    if (usernameCheck)
    {
        System.out.println("User has been succesfully 
deleted.");
    }
    else if (!usernameCheck && !passwordCheck)
    {
        System.out.println("The entered username does not match 
any users.");
    }
}

public void getHint(String username)
{
    Boolean nameCheck = false;
    for(int i=0; i<users.size(); i++)
    {
        if (users.get(i).getUsername().equals(username))
        {
            users.get(i).getPasswordHelp();
            nameCheck = true;
        }
    }
    if (!nameCheck)
    {
        System.out.println("The entered username does not match 
 any users.");
    }
}
}

FacebookUser Class:

package FacebookClass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.*; 
import java.lang.*; 
import java.io.*;

 public class FacebookUser extends UserAccount implements Cloneable, 
 Comparable<FacebookUser>, Serializable {

public FacebookUser(String username, String password) {
    super(username, password);
}

private String passwordHint;
private ArrayList<FacebookUser> friends = new ArrayList<FacebookUser> 
();

void setPasswordHint(String hint)
{
    passwordHint = hint;

}

void friend(FacebookUser newFriend)
{
    // Check if friend is in friends list. If not, add them
    Boolean friendCheck = false;
    for (int i = 0; i < friends.size(); i++)
        {
            if (friends.get(i) == newFriend)
            {
                    friendCheck = true;
            }
        }

        if (friendCheck)
        {

System.out.println(newFriend.getUsername() + " is already are on " + 
this.getUsername() + "'s friends list!");
        }
        else
        {
                friends.add(newFriend);
                System.out.println(this.getUsername() + 
"'s friend list: " + friends);
        }
}

void defriend(FacebookUser formerFriend)
{
    // Check if friend is in friends list. If they are, remove them
    Boolean friendCheck = false;
    for (int i = 0; i < friends.size(); i++)
    {
        if (friends.get(i) == formerFriend)
        {
            friendCheck = true;
        }
    }

    if (!friendCheck)
    {
        System.out.println("They are not on friends list!");
    } 
    else
    {
        friends.remove(formerFriend);
        System.out.println(this.getUsername() + "'s friend 
 list: " + friends);
    }

}

public Object clone() throws CloneNotSupportedException
{
    return super.clone();
}

ArrayList<FacebookUser> getFriends()
{
    // Create clone of face book users array list and return
    ArrayList<FacebookUser> clone = (ArrayList) friends.clone();
    return clone;
}

// Compare to method for sorting
@Override
 public int compareTo(FacebookUser user) {

    return 
this.getUsername().compareToIgnoreCase(user.getUsername());
}

// Abstract get password help method
@Override
public void getPasswordHelp()
{
    if (!(passwordHint == null))
    {
        System.out.println(this.getUsername() + "'s password 
hint is: "+ passwordHint);
    }
    else
    {
        System.out.println(this.getUsername() + "'s password 
hint has not been set");
    };

}

 }

UserAccount Class:

package FacebookClass;

import java.io.Serializable;

import FacebookClass.UserAccount;

public abstract class UserAccount implements Serializable
{

private String username = "";
private String password = "";
private Boolean active = false;

public abstract void getPasswordHelp();

public UserAccount(String username, String password) {
    this.username = username;
    this.password = password;
    active = true;
}

public void createAccount(String username, String password)
{
    this.username = username;
    this.password = password;
    active = true;
}

public boolean checkPassword(String password)
{
    if (password.equals(this.password))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public void deactivateAccount()
{
    active = false;
}

public String toString()
{
    return (username);
}

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

public Boolean getActive() {
    return active;
}
public void setActive(Boolean active) {
    this.active = active;
}

@Override
public int hashCode() 
{
    final int prime = 31;
    int result = 1;
    result = prime * result + ((username == null) ? 0 : 
username.hashCode());
    return result;
}

@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    UserAccount other = (UserAccount) obj;
    if (username == null)
    {
        if (other.username != null)
            return false;
    } else if (!username.equals(other.username))
        return false;
    return true;
}
}
Zach
  • 39
  • 2
  • Possible duplicate of [NoClassDefFoundError: wrong name](https://stackoverflow.com/questions/7509295/noclassdeffounderror-wrong-name) – Ryuzaki L Feb 05 '19 at 20:03
  • 3
    Sounds like you serialized an object, refactored the class that the serializable object was instantiated from, and now the serialized object on your drive doesn't match the class, causing deserialization to fail. Check out [this answer](https://stackoverflow.com/questions/39079928/what-is-the-penalty-for-unnecessarily-implementing-serializable/39088501#39088501) – Vince Feb 05 '19 at 20:04
  • @VinceEmigh Thank you very much! You are my hero, its all fixed now.. One more question, if my instructor runs the program and the file has yet to be created and serialized, will he get the same exception when the deserializing code runs? Should I make it check if the file exists or something? – Zach Feb 05 '19 at 20:26
  • @Zach He's get a `FileNotFound` exception, since your `main` attempts to load the file right away. If he serialized an instance of the class, doesnt modify the class, then tries to deserialize, there shouldn't be any errors – Vince Feb 05 '19 at 23:16
  • @VinceEmigh ok that makes sense. The only problem is he wants the program to deserialize it first by itself so it always has the same data. What would you recommend to do? Could I just catch the file not found exception? When you quit the program it saves the data to the file so it would only catch it that first run. – Zach Feb 05 '19 at 23:54
  • The problem is that the first thing your program does is attempt to deserialize. You don't serialize the object until the user chooses the option, and deserialization is currently happening *before* the user can choose an option. So unless your professor already has a serialized object on their machine before running your program, they'll encounter a `FileNotFoundException`. – Vince Feb 06 '19 at 15:44
  • @VinceEmigh ok, I understand, thanks for the help! – Zach Feb 06 '19 at 19:06

0 Answers0