3

I've a Spring Boot app written in Kotlin that I have deployed to AWS Beanstalk that is throwing the following exception:

Exception in thread "main" java.lang.NoSuchMethodException: com.prototype.demo.DemoApplication.main([Ljava.lang.String;)
at java.base/java.lang.Class.getDeclaredMethod(Class.java:2675)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)

Locally, when I run the app from IntelliJ I've no issues. I have the spring-boot-maven-plugin and the kotlin-maven-plugin in my project.

Any ideas about what could be causing the issue? I previously built a similar app in Java but didn't run into this issue.

Thank you for your expertise and time.


Update: something interesting to notice is that I had to set in the properties of my POM the following line in order to make the project executable by IntelliJ during development (i had forgotten about it): <start-class>com.prototype.demo.DemoApplication</start-class>

Now I've removed it and I get a different error when running the jar file. I will spend some time on it and see if that's related or not and update the question accordingly. Thank you!

Marco
  • 321
  • 3
  • 10
  • 2
    Have you tried running the jar locally rather than via the IDE? – OneCricketeer Aug 21 '22 at 21:23
  • I've run it by doing `java -jar xxx.jar` and i got the same error that I see on AWS. What does this mean? Why is happening? What does IntelliJ do to make it work that I'm unaware of? – Marco Aug 21 '22 at 23:09
  • @OneCricketeer, your comment pointed me in the right direction, thank you so much. – Marco Aug 22 '22 at 08:31

2 Answers2

0

The issue was raised by the following property declared in my POM file: <start-class>com.prototype.demo.DemoApplication</start-class>. Once I removed it and added the open keyword to the Configuration annotated classes and to the DemoApplication(the class containing the main) it finally worked.

Marco
  • 321
  • 3
  • 10
0

Solution for build.gradle.kts

I had the same problem when deploying my application to ECS.

First things first:

When you see such an error in AWS logs -> do as the author of the question has done and test the issue locally. Most probably if the application is failing on AWS, it will also fail on you local machine. To do so, you build a jar locally, the jar file is usually saved in build/libs/application-x.x.x-SNAPSHOT.jar and run java -jar application-x.x.x-SNAPSHOT.jar

Now you can fix your build task and test it locally before deploying it again.

Fix Gradle: My main method was defined in MyApplication.kt file, but in the definition of the main class in gradle I had to set the following:

tasks.getByName<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
    mainClass.set("com.company.app.MyApplicationKt")
}

Notice: MyApplicationKt

That is the only thing I had to change for me to fix this issue.

Diana
  • 935
  • 10
  • 31