简体   繁体   English

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

[英]Could not find or load main class with IntelliJ Application configuration

I have a project in IntelliJ IDEA 2019.1.3 using Gradle, with a main class like this:我在 IntelliJ IDEA 2019.1.3 中有一个使用 Gradle 的项目,主类如下:

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

But every time I try to launch my program, it always shows the same error:但是每次我尝试启动我的程序时,它总是显示相同的错误:

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

I'm using the Application configuration template.我正在使用应用程序配置模板。 I have tried the following:我尝试了以下方法:

  • Clean/Rebuild project清理/重建项目
  • Invalidating caches/restart使缓存无效/重新启动
  • Reimport Gradle project重新导入 Gradle 项目
  • Deleting the .idea folder删除 .idea 文件夹
  • Deleting and recreating the configuration profile删除并重新创建配置文件
  • Recreating the project from scratch从头开始重新创建项目
  • Completely reinstalling IntelliJ完全重新安装 IntelliJ
  • Updating from JDK 8 to JDK 11从 JDK 8 更新到 JDK 11
  • My source sets are correct, the build folder, the classpath module, the file package are correctly set.我的源集是正确的,构建文件夹、类路径模块、文件包都正确设置。 The build task is run before launching.构建任务在启动之前运行。 In fact, there's effectively a Main.class file under the build/ folder and in the generated jar file.实际上,在build/文件夹下和生成的 jar 文件中实际上有一个Main.class文件。

The only solution I know is to run with a gradle task:我知道的唯一解决方案是运行 gradle 任务:

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

But I'd rather not do that since the console won't accept input, doesn't support unicode for some reason, and I can't pass program arguments as easily as with the IntelliJ's configuration window.但我宁愿不这样做,因为控制台不接受输入,出于某种原因不支持 unicode,而且我无法像使用 IntelliJ 的配置窗口那样轻松地传递程序参数。

Has anyone had this problem and how was it fixed?有没有人遇到过这个问题,它是如何解决的? It's only been happening to me since I updated to IntelliJ 2019.1, it worked fine most of the time on 2018.3.6.自从我更新到 IntelliJ 2019.1 以来,它只发生在我身上,它在 2018.3.6 上的大部分时间都运行良好。

EDIT: Gradle settings编辑:Gradle 设置在此处输入图片说明

EDIT 2: The original question was about Kotlin but I realized I get the same error with Java.编辑 2:最初的问题是关于 Kotlin,但我意识到我在使用 Java 时遇到了同样的错误。

When trying to reproduce your libgdx example the very first time everything worked.第一次尝试重现您的 libgdx 示例时,一切正常。 The second attempt to freshly import the project and take screenshots along the way failed like yours.第二次尝试全新导入项目并沿途截取屏幕截图,失败了。 From then on it kept failing.从那以后,它一直失败。

Apparently somehow IntelliJ gets the classpath wrong when importing this project from gradle.显然,从 gradle 导入这个项目时,IntelliJ 以某种方式获取了类路径错误。 It looks for build/java/main instead of build/kotlin/main .它寻找build/java/main而不是build/kotlin/main

To fix the issue Open the Module Settings (F4) of the project and change the "module compile output path" of the modules desktop and core to the kotlin output path.修复问题 打开项目的模块设置(F4),将模块desktopcore的“模块编译输出路径”更改为 kotlin 输出路径。 Just replace the word java in the path with kotlin :只需将路径中的java一词替换为kotlin

模块设置

When you then hit the "run" button next to the main method, it fails like this:当您点击 main 方法旁边的“运行”按钮时,它会失败,如下所示:

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.

This can be fixed by editing the launch configuration and add -XstartOnFirstThread to VM options.这可以通过编辑启动配置并将-XstartOnFirstThread添加到 VM 选项来解决。

The next attempt fails with this exception.下一次尝试失败,出现此异常。

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

When changing the working directory in the launch configuration to the android/assets directory, just like you did in your gradle task, the demo will launch successfully.将启动配置中的工作目录更改为android/assets目录时,就像您在 gradle 任务中所做的那样,演示将成功启动。

For the record my environment:为了记录我的环境:

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

The issue comes from the fact that the project has an Android module.问题来自该项目具有 Android 模块这一事实。 The module needs the android gradle plugin which is globally applied to all modules in the project, even those which aren't android modules.该模块需要 android gradle 插件,该插件全局应用于项目中的所有模块,即使是那些不是 android 模块的模块。 When applied to a module, it adds an Android-Gradle facet that changes the module classpath from the default out/classes to build/classes/java .当应用于模块时,它会添加一个Android-Gradle facet,将模块类路径从默认的out/classes更改为build/classes/java For kotlin modules, the main class can't be found at runtime since the classpath is wrong.对于 kotlin 模块,由于类路径错误,在运行时无法找到主类。

There's is currently no way to apply the android plugin to only android modules.目前无法将 android 插件仅应用于 android 模块。 Workarounds include patching the android.jar file, automatically removing the Android-Gradle facet on sync, or moving the android module to an external project.解决方法包括修补 android.jar 文件、在同步时自动删除 Android-Gradle facet,或将 android 模块移动到外部项目。 There's no clean way to do it.没有干净的方法可以做到这一点。

The issue has already been reported here , but it shows no sign that it will ever be fixed.该问题已在此处报告,但没有迹象表明它会得到修复。

在此处输入图片说明

IntelliJ IDEA free community version download address: Once the download is installed, we can use the tool to create the project. IntelliJ IDEA免费社区版本下载地址:安装下载后,我们可以使用该工具创建项目。 The creation process requires the selection of the SDK, Kotlin and JDK 1.6+. 创建过程需要选择SDK,Kotlin和JDK 1.6+。 can you seee setup kotlin in android here : https://code-android-example.blogspot.com/2019/10/setup-kotlin-android.html 你能在这里看到android中的设置kotlin吗: https ://code-android-example.blogspot.com/2019/10/setup-kotlin-android.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM