Java Swing应用程序导致ClassNotFoundException

问题描述 投票:0回答:1

我目前正在编写Java Swing应用程序。

这是我期望发生的过程:

  1. 用户单击按钮
  2. 多个任务在后台运行
  3. 完成任务后,将通过消息表明过程已完成来更新JTextArea。

我遇到的问题发生在步骤2。其中一个后台进程利用了一个如下所示的枚举:

public enum FilePaths {
    MAIN_AUTH,
    MAIN_AUTH_CREDS,
    MAIN_ACTIONS,
    MAIN_FETCH,
    MAIN_META,
    MAIN_RESRS,
    TEST_AUTH,
    TEST_AUTH_CREDS,
    TEST_ACTIONS,
    TEST_FETCH,
    TEST_META,
    TEST_RESRS;
}

当到达使用此枚举的第一个方法时执行失败,向我显示以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: .../gui/guicontroller/authentication/FilePaths

Caused by: java.lang.ClassNotFoundException: ...gui.guicontroller.authentication.FilePaths

这是Swing中最初启动后台执行的方法:

private class createButtonClicked implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String message = guiController.rename();
            console.append(message);
            if (!guiController.getSessions()) {
                String remove = guiController.remove();
                console.append(removeSessions);
            }
            String addFields = guiController.writeData();
            console.append(addFields);

        }
    }

概述:-控制台:JTextArea添加消息-guiController:视图和模型之间的控制器-named():使用ProcessBuilder(内部使用waitFor())运行bash脚本(不会导致错误!)-remove():导致错误并且第一次使用枚举的方法调用

到目前为止我尝试过的:

  1. 在JUnit测试中运行相同的方法调用-一切正常
  2. [在运行应用程序和单元测试时使用调试器和ClassLoader,在单元测试和应用程序中加载的类数之差为〜300对〜50。
  3. 类加载器在运行应用程序时不会显示枚举,但在运行单元测试时会显示枚举。

编辑:所有代码都是我的,没有拼写错误。已经检查过了。

EDIT2:这是更多代码:

public class GuiController {
    private AuthenticationParser authenticationParser;
    ...
    public String remove() {
        return authenticationParser.remove();
    }
}
import static ...gui.guicontroller.authentication.FilePaths.*;

public class AuthenticationParser {
    ...
    public String remove() {
        String path = GuiUtils.generatePath(MAIN_AUTH_CREDS, name);
        ...
    }
}

remove()中显示的行是导致异常的原因。不会调用方法generatePath()。GuiController位于... gui.guicontroller下。 AuthenticationParser位于... gui.guicontroller.authentication下,与FilePaths相同。

我目前正在编写Java Swing应用程序。这是我期望发生的过程:用户单击一个按钮在后台运行多个任务任务完成后,一个...

java swing classloader swingworker
1个回答
0
投票

如果您说JUnit执行正常,而swing执行会产生ClassNotFound异常,请检查有问题的类(FilePaths)是否位于主源目录中,而不是在测试目录中。

© www.soinside.com 2019 - 2024. All rights reserved.