Gradle - Could not find or load main class

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"
    

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 = '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.


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 and what the gradle build looks like:

Main class:

package com.stormtrident;

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

Notice that it is part of the package "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'
}

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

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