在 IntelliJ IDEA 中运行时出现 java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42660125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:17:14  来源:igfitidea点击:

java.lang.ClassNotFoundException when running in IntelliJ IDEA

javaintellij-idea

提问by urpi

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?

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

enter image description here

在此处输入图片说明

采纳答案by CrazyCoder

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.classfile inside dbsubdirectory of the project output directory (classpath).

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

It can happen for multiple reasons:

它可能由于多种原因发生:

  • wrong main class selected in the run/debug configuration
  • Table.javais 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 Buildstep is excludedfrom from Before launch steps in the Run/Debug configuration
  • project is misconfigured and there is no Source rootdefined for the directorycontaining dbsubdirectory
  • Table.javahas incorrect packagestatement 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 threadfor 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 answerfor more details.
  • In project structure make sure you have the right Java version for compile.
  • There is a known bugthat sometimes a Java project created from the Command Linetemplate doesn't work because .idea/modules.xmlfile references invalid module file named untitled104.iml. Fix the module name manually or create a project from scratch and don't use a template.
  • 在运行/调试配置中选择了错误的主类
  • Table.java被排除在编译之外(偶然或有意,因为它包含错误并且您想在处理其他代码时跳过它)
  • 类未编译,因为构建步骤从运行/调试配置中的启动前步骤中排除
  • 项目配置错误,没有包含子目录的目录定义源根db
  • Table.java有不正确的package陈述或位于/移动到不同的包
  • 项目路径:在 Mac/Linux 上包含一个冒号或;在 Windows上包含分号,它用于分隔类路径并使类路径无效。有关详细信息,请参阅此线程。请注意,Mac 上的 Finder 可能会将路径中的冒号显示为斜杠。
  • 如果依赖的 jar 文件之一进行了数字签名,则 jar 可能不会执行,因为新工件将包含依赖项的部分签名。有关更多详细信息,请参阅此答案
  • 在项目结构中,确保您有正确的 Java 版本进行编译。
  • 有一个已知错误,有时从命令行模板创建的 Java 项目不起作用,因为.idea/modules.xml文件引用了名为untitled104.iml. 手动修复模块名称或从头开始创建项目,不要使用模板。

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

在正确配置的项目和正确的运行/调试配置中,一切正常:

Run

跑

回答by Holger Müller

  • 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.
  • 如果依赖的 jar 文件之一进行了数字签名,则 jar 可能不会执行,因为新工件将包含依赖项的部分签名。有关更多详细信息,请参阅此答案。

I must again emphasis the point CrazyCoder has here.

我必须再次强调 CrazyCoder 在这里的观点。

The (Oracle) JVM used to throw a SecurityExceptionwhen you tried to run a Jar-File containing broken signatures. This made sense from a "What's wrong"-Point of view.

当您尝试运行包含损坏签名的 Jar 文件时,(Oracle) JVM 曾经抛出SecurityException。从“出了什么问题”的角度来看,这是有道理的。

That is no longer the case.They are indeed throwing ClassNotFoundExceptions now - even if the class is right there in the file (no matter if it is in the default package/toplevel or way down in a nested package structure).

这已不再是这种情况。他们现在确实在抛出ClassNotFoundExceptions - 即使该类就在文件中(无论它是在默认包/顶层中还是在嵌套包结构中)。

回答by Phumlani Duncan

Here's what worked for me..

这对我有用..

I deleted .ide folder, .uml file. And all other auto generated files by intelJ i then restarted my ide and i was asked if i want to make my project run with maven that's it.

我删除了 .ide 文件夹,.uml 文件。和所有其他由 intelJ 自动生成的文件,然后我重新启动了我的 ide,我被问到是否要让我的项目与 maven 一起运行,就是这样。

Obviously i said yes.

显然我说是的。