102

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.

1
  • Does not applying application also implicitly applies java plug-in?
    – nabster
    Oct 31, 2022 at 16:07

16 Answers 16

90

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"
    
5
  • 1
    Where do I change sourceSets? In my build.gradle file? Or is there a configuration file that I need to change?
    – kibowki
    Jul 25, 2014 at 15:04
  • 2
    When I add sourceSets.main.java.srcDirs = ['src'] I get an error in the jar task Jun 8, 2019 at 20:49
  • 2
    MissingPropertyException: Could not set unknown property 'mainClassName' for Gradlew 4.1
    – fuat
    Nov 6, 2020 at 8:19
  • 1
    @fuat you need to have the application plugin
    – VFX
    May 2, 2021 at 4:01
  • 1
    Could not set unknown property 'mainClassName' for root project error. Please show full build.gradle file
    – parsecer
    Aug 29, 2022 at 0:53
35

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'
    }
}
0
29

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.

0
10

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'
}
2
  • 1
    For me, adding your jar { manifest { attributes } } bit helped. Thanks!
    – Paul
    Oct 21, 2016 at 20:36
  • What is Netbeans doing differently that Gradle seems to be ran differently? I am attempting this manually so I can understand Gradle better. Netbeans seems to be really good at Maven, but Gradle seems to be fragile in Netbeans. Oct 22, 2019 at 19:01
3

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.

The tutorial I tried with. After you execute gradle build, you will have to be able to find classes under build directory.

1
  • 1
    Also, since no one else is specifying it: the build.gradle file has to be located "alongside" the src/ directory, not along with the .java files in src/main/java/hello/
    – joakimk
    Jan 25, 2016 at 13:45
3

For Netbeans 11 users, this works for me:

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

// This comes out to package + '.' + mainClassName
mainClassName = 'com.hello.JavaApplication1'

Here generally is my tree:

C:\...\NETBEANSPROJECTS\JAVAAPPLICATION1
│   build.gradle
├───src
│   ├───main
│   │   └───java
│   │       └───com
│   │           └───hello
│   │                   JavaApplication1.java
│   │
│   └───test
│       └───java
└───test
2

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

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

2
  • 1
    build/clean + build/build does build build doesn't fix the runtime issue Aug 20, 2018 at 21:56
  • Deleting build saved my life after renaming part of the src tree. Thanks!
    – rwst
    Oct 6 at 15:50
2

If you decided to write your hello.World class in Kotlin, another issue might be that you have to reference it as mainClassName = "hello.WorldKt".

src/main/java/hello/World.kt:

package hello
fun main(args: Array<String>) {
    ...
}
// class World {} // this line is not necessary
1

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

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

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

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

0
  1. verify if gradle.properties define right one 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

enter image description here

0

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

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
1
  • That's already the entry point for a springboot project to start and the question is about configuration conflicts not how to initiate a project
    – aya salama
    Nov 8, 2019 at 14:53
0

For a project structure like

project_name/src/main/java/Main_File.class

in the file build.gradle, add the following line

mainClassName = 'Main_File'
2
  • Welcome to SO!. Please edit your question and elaborate a bit on why your solution is working, i.e. what exactly you are doing.
    – B--rian
    Aug 15, 2019 at 10:50
  • Need full build.gradle file as an example
    – parsecer
    Aug 29, 2022 at 0:52
0

If you're using Spring Boot, this might be the issue: https://github.com/gradle/gradle/issues/2489.

Basically, the output directories changed in Gradle 4.0, so if you have them hardcoded the execution will fail.

The solution is to replace:

bootRun {
   dependsOn pathingJar
   doFirst {
      classpath = files("$buildDir/classes/main", "$buildDir/resources/main", pathingJar.archivePath)
   }
}

by:

bootRun {
   dependsOn pathingJar
   doFirst {
      classpath = files(sourceSets.main.output.files, pathingJar.archivePath)
   }
}
0

I had forgotten to add String[] args inside main method public static void main()

After changing it to public static void main(String[] args), everything worked.

My build.gradle looks like this

apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'hugo.HelloWorld'

FOld stru

0

For Kotlin DSL (build.gradle.kts) use this code:

tasks.jar {
    manifest.attributes["Main-Class"] = "my.package.name.MyClassName"
    // ...
}

Another similar notation:

tasks.jar {
    manifest {
        attributes["Main-Class"] = "my.package.name.MyClassName"
    }
    // ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.