Gradle - Could not find or load main class

kibowki picture kibowki · Jul 24, 2014 · Viewed 146.2k times · Source

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

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.

Here is my build.gradle file:

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.

Answer

kdabir picture kdabir · Jul 24, 2014

I see two problems here, one with sourceSet another with mainClassName.

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

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

    mainClassName = "hello.HelloWorld"