32

I got a problem with my gradle build. I use the standard proposed by the Spring Website (https://spring.io/guides/gs/rest-service/), but when I try to use gradle build, I got this error :

My gradle problem

It doesnt work for this gradle, but when I use another one (that I took when I was at school) it work perfectly.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
mfrachet
  • 8,772
  • 17
  • 55
  • 110

7 Answers7

33

There are two possibilities

  1. Your source directory is not in the right location (use the sourceSets directive to fix this. your source directory should resemble something like src/main/java/your/package)
  2. Add this to indicate where your main class is

    springBoot {
         mainClass = "hello.FileUploader"
    }
    

I am pretty sure it is 1.

roottraveller
  • 7,942
  • 7
  • 60
  • 65
Pradeep
  • 351
  • 3
  • 5
  • 3
    For me, it was 2. :) Once I decided to plug in Spring Boot, I didn't realise I needed to config the main class. Still getting my way around both Gradle and Spring Boot... – Igor Donin Nov 29 '15 at 02:39
  • For me it was number 1: I didn't have `java` in my path -- I had `src/main/my/package` instead of `src/main/java/my/package` – Ryan Heathcote Feb 16 '16 at 20:01
  • I'm joint compiling Java and Groovy with `build.gradle` configured like in this answer: https://stackoverflow.com/a/22164339/185034, in other words I moved the `sourceSets`. #2 worked for me. – Paul Dec 06 '17 at 17:34
23

I also have this problem, Here I solved the problem:

use org.springframework.boot:spring-boot-starter instead of org.springframework.boot:spring-boot-starter-web, if your project is only an module that will be used in other project.

Or Set the main class in gradle:

mainClassName = 'your.package.MainClass'

Or Just disable the bootRepackage

bootRepackage {
    enabled = false
}
Bruce
  • 1,718
  • 20
  • 15
7

There is no main method in your project (otherwise the plugin would find one). A main method has a very specific signature, so check that you have public static void main(String[] args).

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • 4
    This is not true, I am having the same problem and there is a public static void main(String[] args) in my project. – black sensei Oct 18 '14 at 10:52
6

If the main class is not defined in the current project which the build.gradle file belongs to, but you want to launch it for some purpose, like sprint integration test. Do it like this:

Adding

bootRepackage {
    mainClass = 'your.app.package.Application'
}

in build.gradle (after the line apply plugin: 'spring-boot', because the plugin needs to be loaded) fix the problem.

Richard
  • 2,080
  • 18
  • 17
2

I know this is a very old post. But I came across this issue while trying to build my first spring- boot application (https://spring.io/guides/gs/spring-boot/#scratch). So the location of the pom.xml, mentioned in the tutorial is incorrect. You need to place it outside your src folder. So here is the final directory structure - /workspace/src/main/java/hello/HelloController.java /workspace/src/main/java/hello/Application.java /workspace/pom.xml

1

I solved it by specifying the encoding. Probably it's because I wrote the code in an IDE.

java -Dfile.encoding=UTF-8 -jar build <filename>.jar
coyotte508
  • 9,175
  • 6
  • 44
  • 63
surga
  • 1,436
  • 21
  • 25
1

This happened to me as well.

I was confused by the location of build.gradle file: I thought it should be located in src/main/java/hello, because it is mentioned right after the instruction to create this sub-directory structure.

It should be placed in the root folder containing src folder. Once I did that and called "gradle build" from the root folder and not "./gradlew build" as the guide instructs, build was successful.

I did not perform the standard installation for gradle, just downloaded the binaries, maybe this is the reason that "./gradlew build" failed for me.

pringi
  • 3,987
  • 5
  • 35
  • 45
Gidi
  • 7
  • 2