Java Gradle - 无法找到或加载主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24924932/
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-14 15:46:26  来源:igfitidea点击:

Gradle - Could not find or load main class

javaspringgradlebuild.gradle

提问by twilco

I'm trying to run a very simple project using Gradle and running into the following error when using the gradlew run command:

我正在尝试使用 Gradle 运行一个非常简单的项目,并在使用时遇到以下错误gradlew run command

could not find or load main class 'hello.HelloWorld'

could not find or load main class 'hello.HelloWorld'

Here is my file structure:

这是我的文件结构:

SpringTest
    -src
        -hello
            -HelloWorld.java
            -Greeter.java
    -build
         -libs
         -tmp
    -gradle
         -wrapper
    -build.gradle
    -gradlew
    -gradlew.bat

I excluded the contents of the libs and tmp folders because I didn't think that would be relevant information for this issue, but I can add it in if need be.

我排除了 libs 和 tmp 文件夹的内容,因为我认为这不是此问题的相关信息,但如果需要,我可以将其添加。

Here is my build.gradle file:

这是我的 build.gradle 文件:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'

mainClassName = 'hello/HelloWorld'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile "joda-time:joda-time:2.2"
}

jar {
    baseName = "gs-gradle"
    version = "0.1.0"
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

Any idea on how to fix this issue? I've tried all sorts of things for the mainClassName attribute but nothing seems to work.

关于如何解决这个问题的任何想法?我已经为 mainClassName 属性尝试了各种方法,但似乎没有任何效果。

采纳答案by kdabir

I see two problems here, one with sourceSetanother with mainClassName.

我在这里看到两个问题,一个sourceSetmainClassName.

  1. Either move java source files to src/main/javainstead of just src. Or set sourceSetproperly by adding the following to build.gradle.

    sourceSets.main.java.srcDirs = ['src']
    
  2. mainClassNameshould be fully qualified class name, not path.

    mainClassName = "hello.HelloWorld"
    
  1. 上移java源文件src/main/java,而不是只src。或者sourceSet通过将以下内容添加到 build.gradle 来正确设置。

    sourceSets.main.java.srcDirs = ['src']
    
  2. mainClassName应该是完全限定的类名,而不是路径。

    mainClassName = "hello.HelloWorld"
    

回答by hepifish

I just ran into this problem and decided to debug it myself since i couldn't find a solution on the internet. All i did is change the mainClassName to it's whole path(with the correct subdirectories in the project ofc)

我刚遇到这个问题并决定自己调试它,因为我在互联网上找不到解决方案。我所做的就是将 mainClassName 更改为它的整个路径(在项目 ofc 中使用正确的子目录)

    mainClassName = 'main.java.hello.HelloWorld'

I know it's been almost one year since the post has been made, but i think someone will find this information useful.

我知道帖子发布已经快一年了,但我认为有人会发现这些信息很有用。

Happy coding.

快乐编码。

回答by Noumenon

Modify build.gradle to put your main class in the manifest:

修改 build.gradle 将您的主类放在清单中:

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version,
                   'Main-Class': 'hello.helloWorld'
    }
}

回答by Dino Tw

Struggled with the same problem for some time. But after creating the directory structure src/main/java and putting the source(from the top of the package), it worked as expected.

为同样的问题苦苦挣扎了一段时间。但是在创建目录结构 src/main/java 并放置源代码(从包的顶部)之后,它按预期工作。

The tutorialI tried with. After you execute gradle build, you will have to be able to find classesunder builddirectory.

我试过的教程。执行后gradle build,您必须能够classesbuild目录下找到。

回答by Psi-Ed

In my build.gradle, I resolved this issue by creating a task and then specifying the "mainClassName" as follows:

在我的 build.gradle 中,我通过创建一个任务然后指定“mainClassName”来解决这个问题,如下所示:

task(runSimpleXYZProgram, group: 'algorithms', description: 'Description of what your program does', dependsOn: 'classes', type: JavaExec) {
    mainClassName = 'your.entire.package.classContainingYourMainMethod'
}

回答by Nav

Just to make it clear for newbies trying to run a gradle project from Netbeans:
To understand this, you need to see what the main class name looks like andwhat the gradle build looks like:

只是为了让尝试从 Netbeans 运行 gradle 项目的新手清楚:
要理解这一点,您需要查看主类名称的外观以及gradle 构建的外观:

Main class:

主要类:

package com.stormtrident;

public class StormTrident {
    public static void main(String[] cmdArgs) {
    }
}

Notice that it is part of the package "com.stormtrident".

请注意,它是“com.stormtrident”包的一部分。

Gradle build:

摇篮构建:

apply plugin: 'java'

defaultTasks 'jar'

jar {
 from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }    
    manifest {
        attributes 'Main-Class': 'com.stormtrident.StormTrident'
    }
}


sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.stormtrident.StormTrident'
}

repositories {
    mavenCentral()
}

dependencies {
    //---apache storm
    compile 'org.apache.storm:storm-core:1.0.0'  //compile 
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

回答by JohnTheBeloved

I fixed this by running a clean of by gradle build (or delete the gradle build folder mannually)

我通过运行清理 by gradle build(或手动删除 gradle build 文件夹)解决了这个问题

This occurs if you move the main class to a new package and the old main class is still referenced in the claspath

如果您将主类移动到新包并且在 claspath 中仍然引用旧的主类,则会发生这种情况

回答by RichArt

When I had this error, it was because I didn't have my class in a package. Put your HelloWorld.javafile in a "package" folder. You may have to create a new package folder:

当我遇到这个错误时,是因为我的类没有放在 package 中。将您的HelloWorld.java文件放在“包”文件夹中。您可能需要创建一个新的包文件夹:

Right click on the hellofolder and select "New" > "Package". Then give it a name (e.g: com.example) and move your HelloWorld.javaclass into the package.

右键单击hello文件夹并选择“新建”>“包”。然后给它一个名字(例如:com.example)并将你的HelloWorld.java类移动到包中。

回答by cane

  1. verify if gradle.propertiesdefine right one JAVA_HOVE

    org.gradle.java.home=C:\Program Files (x86)\Java\jdk1.8.0_181

  1. 验证gradle.properties是否定义了正确的 JAVA_HOVE

    org.gradle.java.home=C:\Program Files (x86)\Java\jdk1.8.0_181

or

或者

  1. if it's not defined be sure if Eclipse know JDK and not JRE
  1. 如果未定义,请确定 Eclipse 是否知道 JDK 而不是 JRE

enter image description here

在此处输入图片说明

回答by Premakumar

I resolved it by adding below code to my application.

我通过将以下代码添加到我的应用程序来解决它。

// enter code here this is error I was getting when I run build.gradle. main class name has not been configured and it could not be resolved

// 在此处输入代码 这是我运行 build.gradle 时遇到的错误。主类名未配置,无法解析

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}