我需要这个简单的虚拟银行程序,以便在每次启动时对主帐户进行反序列化。我已经完成了这个,但运行此方法的GUI类继续给我一个java.lang.ClassNotFoundException
。我不确定我的错误是什么或如何解决它,我该怎么办?我将在下面发布两个类,因为我不确定错误发现的位置。
BankGUI课程:
package GUIs;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import virtualBank3.BankAccount;
@SuppressWarnings("serial")
public class BankGUI implements Serializable {
BankAccount account;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
BankGUI gui = new BankGUI();
gui.deserializeAccount();
gui.displayGUI();
}
// all global components for JFrame
JTextArea statusArea;
JCheckBox isLockedCheckBox;
JList depositAmount;
JList withdrawAmount;
JButton depositButton;
JButton withdrawButton;
JButton saveAccountButton;
private void displayGUI() {
JFrame frame = new JFrame("Virtual Bank v3.3");
Integer[] intList = { 1, 2, 5, 10, 20, 50, 100, 150, 250, 500 };
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBackground(Color.GREEN);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.GREEN);
centerPanel.setLayout(new GridBagLayout());
frame.add(BorderLayout.CENTER, centerPanel);
frame.add(BorderLayout.EAST, rightPanel);
// add some JLabel's
JLabel depositAmountLabel = new JLabel("Deposit Amount:");
JLabel withdrawAmountLabel = new JLabel("Withdraw Amount:");
JLabel isLockedLabel = new JLabel("Lock account(True/False)");
// finish components(center panel)
statusArea = new JTextArea(15, 22);
statusArea.setEditable(false);
// add this to panel
isLockedCheckBox = new JCheckBox();
// add this to panel
// scrollers and Jlists
//***********************************************************************
depositAmount = new JList(intList);
JScrollPane scroller1 = new JScrollPane(depositAmount);
scroller1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
depositAmount.setVisibleRowCount(1);
depositAmount.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
withdrawAmount = new JList(intList);
JScrollPane scroller2 = new JScrollPane(depositAmount);
scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
depositAmount.setVisibleRowCount(1);
depositAmount.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//***********************************************************************
depositButton = new JButton("Deposit Amount.");
withdrawButton = new JButton("Withdraw Amount");
saveAccountButton = new JButton("Save your Account");
frame.setSize(425, 650);
frame.setVisible(true);
}
private void serializeAccount() {
try {
ObjectOutputStream objectStream1 = new ObjectOutputStream(
new FileOutputStream("bankAccounts.txt"));
objectStream1.writeObject(account);
objectStream1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void deserializeAccount() {
try {
ObjectInputStream objectStream2 = new ObjectInputStream(
new FileInputStream("bankAccounts.txt"));
account = (BankAccount) objectStream2.readObject();
objectStream2.close();
//test it
System.out.println("It worked.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
BankAccount类:
package virtualBank3;
import java.io.Serializable;
public class BankAccount implements Serializable {
private static final long serialVersionUID = -5341449653011848470L;
int balance = 0;
int userWallet = 0;
String owner = "Hello, world";
String accountStat = "Account Owner: " + owner + "\nAccount balance: $"
+ balance + "\nOwner Wallet Balance: $" + userWallet;
Boolean isLocked = false;
public int withdraw(int amount) {
balance -= amount;
userWallet += amount;
return userWallet;
}
public int deposit(int amount) {
balance += amount;
userWallet -= amount;
return balance;
}
public int depositCashIntoWallet(int amount) {
userWallet += amount;
return userWallet;
}
}
这是错误:
java.lang.ClassNotFoundException: virtualBank3.Account
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 GUIs.BankGUI.deserializeAccount(BankGUI.java:123)
at GUIs.BankGUI.main(BankGUI.java:40)
答案 0 :(得分:3)
这是一个命名问题 - 您有public class virtualBank3.BankAccount
,但序列化文件引用virtualBank3.Account
。您是否编写了序列化文件,或者是否已将其提供给您?如果是前者,则重命名一个类,但没有重新创建序列化文件。
答案 1 :(得分:2)
根据我上面的评论,这些评论现在已经丢失了:
@HashSlingingHacker您是否已将类名从Account更改为BankAccount和序列化数据?然后,当试图重新读取序列化数据时,它正在寻找一个Account类(不再存在)。这可能吗?你在哪里有virtualBank3.Account类?
正如KPZ所说,在您的stacktrace中引用了Account.class:
java.lang.ClassNotFoundException: virtualBank3.Account
它反序列化您的数据,但找到“Account.class”序列化对象。您已经将代码更改为使用BankAccount,因此该类不再存在。我将删除所有序列化文件,以确保它们使用您的BankAccount类进行序列化。
答案 2 :(得分:1)
我假设你正在研究eclipse。我猜您已将类 Account
重命名为BankAccount
。
尝试:
答案 3 :(得分:1)
就这样做
项目--->清洁--->清除所有项目
因为您将类帐户重命名为BankAccount