我需要更新我的项目,我在类上使用了小写字母,而不是标准的编码约定。因此,我在eclipse中使用了折射器>重命名类。现在我得到了这个异常/错误:
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)
我使用了折射镜,并勾选了更改所有名称的框,我不知道问题出在哪里。
驱动程序类:
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类:
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类:
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类:
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;
}
}