java - 无法使用 IntelliJ 应用程序配置找到或加载主类

标签 java intellij-idea kotlin jvm

我在 IntelliJ IDEA 2019.1.3 中有一个使用 Gradle 的项目,主类如下:

public final class Main {
    public static void main(String[] args) {
        // Do stuff
    }
}

但是每次我尝试启动我的程序时,它总是显示相同的错误:

Error: Could not find or load main class com.example.Main
Caused by : java.lang.ClassNotFoundException: com.example.Main

我正在使用应用程序配置模板。我尝试了以下方法:

  • 清理/重建项目
  • 使缓存失效/重启
  • 重新导入 Gradle 项目
  • 删除 .idea 文件夹
  • 删除并重新创建配置文件
  • 从头开始重新创建项目
  • 完全重新安装 IntelliJ
  • 从 JDK 8 更新到 JDK 11
  • 我的源设置是正确的,构建文件夹,类路径模块,文件包设置正确。构建任务在启动之前运行。实际上,在 build/ 文件夹和生成的 jar 文件中实际上有一个 Main.class 文件。

我知道的唯一解决方案是使用 gradle 任务运行:

task run(type: JavaExec) {
    main = "com.example.MainKt"
    classpath = sourceSets.main.get().runtimeClasspath
    standardInput = System.in
    isIgnoreExitValue = true
}

但我宁愿不这样做,因为控制台不接受输入,由于某种原因不支持 unicode,而且我不能像使用 IntelliJ 的配置窗口那样轻松地传递程序参数。

有人遇到过这个问题吗?是如何解决的?自从我更新到 IntelliJ 2019.1 后,它才发生在我身上,大部分时间在 2018.3.6 上运行良好。

编辑:Gradle 设置 enter image description here

编辑 2:最初的问题是关于 Kotlin,但我意识到我在使用 Java 时遇到了同样的错误。

最佳答案

第一次尝试重现您的 libgdx 示例时,一切正常。第二次尝试新导入项目并沿途截取屏幕截图像​​您一样失败。从那以后,它一直失败。

显然,从 gradle 导入此项目时,IntelliJ 以某种方式获取了错误的类路径。它寻找 build/java/main 而不是 build/kotlin/main

解决问题打开项目的模块设置(F4),将模块desktopcore的“模块编译输出路径”更改为kotlin输出小路。只需将路径中的单词 java 替换为 kotlin:

module settings

当你点击主方法旁边的“运行”按钮时,它会像这样失败:

Caused by: java.lang.IllegalStateException: GLFW windows may only be created on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. For offscreen rendering, make sure another window toolkit (e.g. AWT or JavaFX) is initialized before GLFW.

这可以通过编辑启动配置并将 -XstartOnFirstThread 添加到 VM 选项来解决。

下一次尝试失败并出现此异常。

com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: libgdx.png

将启动配置中的工作目录更改为 android/assets 目录时,就像您在 gradle 任务中所做的那样,演示将成功启动。

记录一下我的环境:

OS: MacOs Mojave
JVM: openjdk version "1.8.0_212" (AdoptOpenJDK)
IntelliJ: 2019.1.3 Ultimate Edition

关于java - 无法使用 IntelliJ 应用程序配置找到或加载主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305318/

相关文章:

java - 缓存控制在 Firefox 中不起作用

java - 是否可以解析这些日期

xml - 如何在 IntelliJ IDEA 中加载 xml 文件

generics - Kotlin 类型与泛型不匹配

android - List Adapter Diff Util 不更新 Recyclerview 中的列表项

postgresql - jOOQ 查询表达式类型安全,无需自定义转换器

java - XML 中默认条目的 Android Spinner 项目布局

java - 如何在同一个jvm中运行两个或多个应用程序

java - 我如何告诉 IntelliJ 使用 Java 1.6 JDK 启动 gradle?

intellij-idea - IntelliJ 想法 - 发现 Jetty 重复配置?