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

发布于 2024-10-17 07:20:25 字数 2335 浏览 1 评论 0原文

我已经编写了五个类,简而言之,它们的目的是创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2024-10-24 07:20:25

至于告诉java在哪里可以找到ConferenceHandler(我很抱歉听起来很愚蠢 - 作业问题是新手问的)我正在使用Blue Jay,我所要做的就是单击主程序,程序就会运行。我不知道如何从命令行执行此操作。

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


我意识到这是类路径的问题,无论我做什么,都无法修复它。因此,我获取了所有原始类,并将它们全部粘贴到一个新文件夹中,这非常有效。

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

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

堆栈跟踪另有指示。它们是无包的(换句话说,在默认包中,如果 BlueJ 确实以某种方式告诉您的话)。 – @BalusC

我希望我知道这意味着什么...... - @unit

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

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

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

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

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.

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.


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.

That's a Voodoo solution. You really need to understand the problem.

To that end, lets revisit some comments above:

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

What @BalusC means is that the stack trace tells you the full name of the class that it was trying to load.

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

And the full name (in this case) is ConsoleHandler ... not some.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文