java - 程序编译正常,但因 NoClassDefFoundError 崩溃

标签 java bluej

我已经编写了五个类,简而言之,它们的目的是创建 GUI 并记录 session 的参与者。我已经构建了面板并添加了它们:FocusListeners、ActionListeners、ItemListeners。

据我所知,崩溃时收到的消息与我的类路径有关,但我真的不知道如何修复它。这是发生崩溃的代码(当我为面板的两个按钮添加 ActionListener 时:

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)

我知道这里有很多人都有丰富的经验,但我在互联网上找不到任何关于这方面的帮助。

最佳答案

As for telling java where to find ConferenceHandler (and I apologize for sounding stupid - homework questions are asked by newbs) I am using Blue Jay, and all I have to do is click on the main, and the program runs. I have no idea how to do this from command line.

要查找的地方是 java 命令的文档;例如here 。请特别注意有关类路径及其不同设置方法的内容。另请阅读this page .


I realized that it was a problem with classpath, and no matter what I did, I couldn't fix it. So I took all of the original classes, and pasted them all into a new folder, and it worked like a charm.

这是一个 Voodoo 解决方案。你确实需要理解这个问题。

为此,让我们回顾一下上面的一些评论:

The stacktrace indicates otherwise. They're packageless (in other words, in the default package, if BlueJ is telling that you literally somehow). – @BalusC

I wish I knew what that meant..... - @unit

@BalusC 的意思是堆栈跟踪告诉您它尝试加载的类的全名。

    ...
    Caused by: java.lang.ClassNotFoundException: ConferenceHandler
    ...

全名(在本例中)是 ConsoleHandler ...而不是 some.pkg.ConsoleHandler

尚不完全清楚的是为什么会发生这种情况,但我怀疑您实际上运行的是旧的 .class 文件,这些文件与您所使用的源代码不匹配看着。您的 Voodoo 解决方案很可能通过用新内容替换旧的 .class 文件来“修复”此问题。但如果是这种情况,您需要了解您的构建/部署方式有什么问题。否则,你会一次又一次被这种事情绊倒。

关于java - 程序编译正常,但因 NoClassDefFoundError 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4982502/

相关文章:

java - JNI 访问数组的数组

java - 我试图将 txt 文件读入数组,但得到 OutOfBoundsException(0)

java - 无法从 HashSet 中的类调用方法

java - 错误表示它除以零,但未初始化为零

java - BlueJ/Java 程序返回一个新数组,其中包含其参数的组件总和

java.util.Sublist 抛出 StackOverFlowError

java - 如何保存文本文件而不必每次都输入文件名?

java - 使用泛型类实现 Comparable

java - 通过Hibernate获取数据而不传递主键

java - 为什么我们在阿姆斯壮程序中写n/=10?