9

I am currently upgrading from Grails 3.3.10 to Grails 4.0.0. When attempting to run my project I get the following error:

 Execution failed for task ':bootRun'.
 15:31:02.101 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Unable to find a single main class from the following candidates [com.torque.Application, com.base.torque.ExtendedReportsController, com.base.torque.utils.NaturalOrderComparator]

Whether I try to run the project in my IDE or my terminal, I get the same error.

I uninstalled and reinstalled grails 4.0.0 and I still get the error. Tried ./gradlew clean, rebuilding, killall -9 java and still get the error.

This was not an issue with previous Grails versions. No clue what is causing the issue with Grails 4 and no clue how to solve. Any insight towards a solution would be appreciated.

  • Do you have a `main` method in `com.base.torque.utils.NaturalOrderComparator` and `com.base.torque.ExtendedReportsController`? – Jeff Scott Brown Aug 19 '19 at 20:34
  • It would be very strange to have a `main` method in a controller or a comparator, but the error message suggests that is the case. The answer I posted below describes how to make the error go away. If the error is happening and you don't have `main` methods in those classes, I think that would represent a bug. If that is the case, please file an issue at https://github.com/grails/grails-core/issues and we can investigate. – Jeff Scott Brown Aug 19 '19 at 20:47
  • @JeffScottBrown there is a main method in com.base.torque.utils.NaturalOrderComparator but not in the com.base.torque.ExtendedReportsController. – No Longer Rooky Coder Aug 19 '19 at 20:52
  • It looks like Spring Boot is confused and thinks there is a `main` method in `com.base.torque.ExtendedReportsController`. – Jeff Scott Brown Aug 19 '19 at 20:57
  • @JeffScottBrown - that worked. Thank you. – No Longer Rooky Coder Aug 19 '19 at 21:12
  • 2
    I had this in Grails 4. After I did `grails clean` the error was gone. – A.W. Oct 16 '19 at 09:41
  • 1
    @August, yeah, this works for me. – FrEqDe Aug 27 '21 at 18:50

2 Answers2

15

If you have multiple classes which contain a main method, you can disambiguate with something like this in your build.gradle:

springBoot {
    mainClassName = 'com.torque.Application'
}
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • 1
    I saw lots of recommendations and suggestions, but only this worked for me. – tylerwal Aug 20 '19 at 19:09
  • 1
    This was happening to me pretty much anytime I edited `application.groovy` or `changelog.groovy`. I assumed it had something to do with those being groovy scripts instead of classes. I was using the "clean" option and then the next run would work. This modification worked great. – burns Nov 13 '20 at 02:32
  • 1
    `"I assumed it had something to do with those being groovy scripts instead of classes."` - Makes sense. The issue is related to any classes that have a `main` method in them, and all Groovy scripts have a main method in them (even though you don't see it in the source code). Glad you got it worked out! – Jeff Scott Brown Nov 13 '20 at 13:09
2

This solved it for me on Grails 5.2.2:

springBoot {
    mainClass = "com.mypackage.Application"
}
rchfox
  • 159
  • 1
  • 4