Gradle - 无法找到或加载主类

twi*_*lco 62 java spring gradle build.gradle

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

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

这是我的文件结构:

SpringTest
    -src
        -hello
            -HelloWorld.java
            -Greeter.java
    -build
         -libs
         -tmp
    -gradle
         -wrapper
    -build.gradle
    -gradlew
    -gradlew.bat
Run Code Online (Sandbox Code Playgroud)

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

这是我的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'
}
Run Code Online (Sandbox Code Playgroud)

有关如何解决此问题的任何想法?我已经为mainClassName属性尝试了各种各样的东西,但似乎没有任何效果.

kda*_*bir 62

我在这里看到两个问题,一个与sourceSet另一个问题mainClassName.

  1. 将java源文件移动到src/main/java而不仅仅是src.或者sourceSet通过将以下内容添加到build.gradle来正确设置.

    sourceSets.main.java.srcDirs = ['src']
    
    Run Code Online (Sandbox Code Playgroud)
  2. mainClassName 应该是完全限定的类名,而不是路径.

    mainClassName = "hello.HelloWorld"
    
    Run Code Online (Sandbox Code Playgroud)

  • 当我添加`sourceSets.main.java.srcDirs = ['src']`时,jar任务出现错误 (2认同)
  • `MissingPropertyException:无法为 Gradlew 4.1 设置未知属性'mainClassName'` (2认同)

Nou*_*non 27

修改build.gradle以将主类放入清单中:

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version,
                   'Main-Class': 'hello.helloWorld'
    }
}
Run Code Online (Sandbox Code Playgroud)


hep*_*ish 19

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

    mainClassName = 'main.java.hello.HelloWorld'
Run Code Online (Sandbox Code Playgroud)

我知道这篇文章已经发布差不多一年了,但我认为有人会发现这些信息很有用.

快乐的编码.


Nav*_*Nav 8

只是要清楚新手试图运行从NetBeans中gradle这个项目:
要理解这一点,你需要看到什么主要的类名看起来像什么gradle这个构建的样子:

主要课程:

package com.stormtrident;

public class StormTrident {
    public static void main(String[] cmdArgs) {
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,它是包"com.stormtrident"的一部分.

Gradle构建:

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'
}
Run Code Online (Sandbox Code Playgroud)