在 IntelliJ IDEA 中运行时出现 java.lang.ClassNotFoundException [英] java.lang.ClassNotFoundException when running in IntelliJ IDEA

查看:126
本文介绍了在 IntelliJ IDEA 中运行时出现 java.lang.ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序来处理数据库,但在 IntelliJ IDEA 中编译时出现以下错误.有没有人为什么会发生这种情况,我该如何解决?

解决方案

您遇到的错误不是在编译时发生,而是在您尝试运行应用程序时发生.这是因为 Java 无法在项目输出目录(类路径)的 db 子目录中找到 Table.class 文件.

这可能有多种原因:

  • 错误的主类

    I creating a program to work with databases and I am getting the following error when compiling in IntelliJ IDEA. Does anyone why this is happening and how I could solve it?

    解决方案

    The error that you get occurs not on complilation, but when you try to run your application. It happens because Java was not able to find Table.class file inside db subdirectory of the project output directory (classpath).

    It can happen for multiple reasons:

    • wrong main class selected in the run/debug configuration
    • Table.java is excluded from compilation (by accident or intentionally because it contained errors and you wanted to skip it while working on other code)
    • class not compiled because Build step is excluded from from Before launch steps in the Run/Debug configuration
    • project is misconfigured and there is no Source root defined for the directory containing db subdirectory
    • Table.java has incorrect package statement or is located/moved to a different package
    • project path contains a colon : on Mac/Linux or semicolon ; on Windows, it's used to separate the classpath and will render the classpath invalid. See this thread for details. Note that Finder on Mac may display colons in the path as slashes.
    • the jar may not execute if one of the dependent jars is digitally signed since the new artifact will include the partial signature of the dependency. See this answer for more details.
    • In project structure make sure you have the right Java version for compile.
    • there is a known bug that sometimes a Java project created from the Command Line template doesn't work because .idea/modules.xml file references invalid module file named untitled104.iml. Fix the module name manually or create a project from scratch and don't use a template.
    • on Windows "Beta: Use Unicode UTF-8 for worldwide language support" Region Setting is enabled. See IDEA-247837 for more details and workarounds.
    • When IntelliJ IDEA is configured to store module dependencies in Eclipse format source root configuration is lost due to a known bug. Configure the module to use IntelliJ IDEA format dependencies as a workaround.

    In a properly configured project and with the correct run/debug configuration everything works just fine:

    这篇关于在 IntelliJ IDEA 中运行时出现 java.lang.ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆