程序编译正常,但因 NoClassDefFoundError 崩溃
我已经编写了五个类,简而言之,它们的目的是创建 GUI 并记录会议的参与者。我已经构建了面板并添加了它们:FocusListeners、ActionListeners、ItemListeners。
据我所知,崩溃时收到的消息与我的类路径有关,但我真的不知道如何修复它。这是发生崩溃的代码(当我为面板的两个按钮添加 ActionListeners 时:
private void buildButtonPanel()
{
//create the buttonpanel
buttonPanel = new JPanel(new FlowLayout());
//create the buttons
calculate = new JButton("Calculate Charges");
clear = new JButton ("Clear");
//add listeners to the buttons
ConferenceHandler handler = new ConferenceHandler(this);
calculate.addActionListener(handler); //crash occurs on this line
clear.addActionListener(handler);
//create a text area
JTextArea textArea = new JTextArea(5,30);
textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
//add a scrollbar to the textarea
JScrollPane scroll = new JScrollPane (textArea);
//add everything to the buttonpanel
buttonPanel.add(calculate); buttonPanel.add(clear); buttonPanel.add(scroll);
}
我收到的崩溃消息:
java.lang.NoClassDefFoundError: ConferenceHandler
at ConferenceGUI.buildButtonPanel(ConferenceGUI.java:63)
at ConferenceGUI.<init>(ConferenceGUI.java:33)
at IsItWorking.<init>(IsItWorking.java:16)
at IsItWorking.main(IsItWorking.java:28)
Caused by: java.lang.ClassNotFoundException: ConferenceHandler
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at ConferenceGUI.buildButtonPanel(ConferenceGUI.java:63)
at ConferenceGUI.<init>(ConferenceGUI.java:33)
at IsItWorking.<init>(IsItWorking.java:16)
at IsItWorking.main(IsItWorking.java:28)
at __SHELL0.run(__SHELL0.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
我知道这里有很多人有丰富的经验,但我找不到互联网上对此有任何帮助。
I have written five classes, which, in a nutshell, serve the purpose of creating a GUI and recording participants for a conference. I have built panels and added them, FocusListeners, ActionListeners, ItemListeners.
From what I can gather, the message I am getting upon crash is related to my Classpath, but I don't really know how to fix it. Here is the code where the crash takes place (it is when I add the ActionListeners for the two buttons for my panel:
private void buildButtonPanel()
{
//create the buttonpanel
buttonPanel = new JPanel(new FlowLayout());
//create the buttons
calculate = new JButton("Calculate Charges");
clear = new JButton ("Clear");
//add listeners to the buttons
ConferenceHandler handler = new ConferenceHandler(this);
calculate.addActionListener(handler); //crash occurs on this line
clear.addActionListener(handler);
//create a text area
JTextArea textArea = new JTextArea(5,30);
textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
//add a scrollbar to the textarea
JScrollPane scroll = new JScrollPane (textArea);
//add everything to the buttonpanel
buttonPanel.add(calculate); buttonPanel.add(clear); buttonPanel.add(scroll);
}
The crash message I get:
java.lang.NoClassDefFoundError: ConferenceHandler
at ConferenceGUI.buildButtonPanel(ConferenceGUI.java:63)
at ConferenceGUI.<init>(ConferenceGUI.java:33)
at IsItWorking.<init>(IsItWorking.java:16)
at IsItWorking.main(IsItWorking.java:28)
Caused by: java.lang.ClassNotFoundException: ConferenceHandler
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at ConferenceGUI.buildButtonPanel(ConferenceGUI.java:63)
at ConferenceGUI.<init>(ConferenceGUI.java:33)
at IsItWorking.<init>(IsItWorking.java:16)
at IsItWorking.main(IsItWorking.java:28)
at __SHELL0.run(__SHELL0.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
I know there are a lot of people here with a lot of experience, and I cannot find any help on the interwebs on this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要查找的地方是 java 命令的文档;例如此处。请特别注意有关类路径及其不同设置方法的内容。另请阅读此页面。
这是一个 Voodoo 解决方案。你确实需要了解这个问题。
为此,让我们回顾一下上面的一些评论:
@BalusC 的意思是堆栈跟踪告诉您它试图加载的类的全名。
全名(在本例中)是
ConsoleHandler
...而不是some.pkg.ConsoleHandler
。目前尚不完全清楚的是为什么会发生这种情况,但我怀疑您实际上运行的是与您正在查看的源代码不匹配的旧
.class
文件。您的 Voodoo 解决方案很可能通过用新内容替换旧的.class
文件来“修复”此问题。但如果是这种情况,您需要了解您的构建/部署方式有什么问题。不然你会一次又一次被这种事情绊倒。The place to look is in the documentation for the
java
command; e.g. here. Pay particular attention to the stuff that talks about the classpath and the different ways of setting it. Also read this page.That's a Voodoo solution. You really need to understand the problem.
To that end, lets revisit some comments above:
What @BalusC means is that the stack trace tells you the full name of the class that it was trying to load.
And the full name (in this case) is
ConsoleHandler
... notsome.pkg.ConsoleHandler
.What is not entirely clear is why this is occurring, but I suspect that you were actually running old
.class
files that didn't match the source code you were looking at. And your Voodoo solution could well have "fixed" this by replacing the old.class
files with new stuff. But if that's the case, you need to understand what is wrong with the way you are building / deploying. Otherwise, you'll trip over this kind of thing over and over again.