The error “Could not find or load main class” can occur when you’re trying to build a REST Spring app with Gradle. There are a few things you can check to try to fix this error: Gradle building rest spring app can’t find main class
- Make sure that the main class for your application is defined in the
build.gradle
file. The main class is typically the class that contains themain()
method. - Make sure that the main class is located in the
src/main/java
directory. - Make sure that the main class has a public
main()
method. - Make sure that the
main()
method has the following signature:
public static void main(String[] args)
If you’ve checked all of these things and you’re still getting the error, you can try the following:
- Run
gradle clean
to clean the project’s build files. - Run
gradle build
to rebuild the project. - If you’re still getting the error, you can try asking for help on the Gradle forum or on Stack Overflow.
Here are some additional tips that may help you fix the “Could not find or load main class” error:
- Make sure that you’re using the correct version of Gradle.
- Make sure that you’re using the correct version of the Spring Boot starter dependency.
- Make sure that your project is configured correctly.
- Make sure that you’re not making any common mistakes.
I kept getting the follow error message. how to fix it.
FAILURE: Build failed with an exception.
- What went wrong: Execution failed for task ‘:findMainClass‘. > org.gradle.api.tasks.SourceSetOutput.getClassesDir()Ljava/io/File;
plugins {
id ‘java’
id ‘idea’
id ‘eclipse’
id ‘org.springframework.boot’ version ‘2.1.7.RELEASE’
}
group ‘com.teamtreehouse’
version ‘1.0-SNAPSHOT’
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile ‘org.springframework.boot:spring-boot-starter-web:2.1.7.RELEASE’
}
Answers
springBoot {
mainClass = “main.java.hello.Application”
}
Note that mainclass add your package name and main function name
both ./gradlew clean bootRun and ./gradlew clean build with java -jar build/libs/gs-rest-service-0.1.0.jar work well.
Good Luck