I’m no Spring Boot expert. Far from it. However, in the project I am working on at the moment, there is a Spring Boot API created using Kotlin that I need to support.
Part of that challenge is just getting the API up and running. I’ve had a few days of head scratching with this one, but finally made some headway this afternoon.
So here’s what I was doing:
I open IntelliJ and start the app using the “Select Run / Debug Configuration”. In there I have a preset Gradle task that when I click the Big Green Play Sign ™, I was getting the following:
Execution failed for task ':bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-18.0.2.1.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':bootRun'.
Caused by: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk-18.0.2.1.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
at org.springframework.boot.gradle.tasks.run.BootRun.exec(BootRun.java:90)
Code language: Shell Session (shell)
As part of this, I could click the option to run with --info
or --debug
, and not shown above but another one I would get is --stacktrace
. In fact, I think that is the flag I used in the command above. I just truncated the output.
Basically the error above is really … useless?
“Execution failed for task … java finished with non-zero exit value 1”
Great.
But why?
Well, I tried adding breakpoints around the app, but it didn’t seem to be doing very much at all.
Here’s what I had:
class MyKotlinSpringBootApplication {
fun main(args: Array<String>) {
SpringApplication.run(MyKotlinSpringBootApplication::class.java, *args)
}
}
Code language: Kotlin (kotlin)
But what really helped dive in further was to wrap that main application class in a try
/ catch
block.
To do this in Kotlin I needed the following:
class MyKotlinSpringBootApplication {
fun main(args: Array<String>) {
try {
SpringApplication.run(MyKotlinSpringBootApplication::class.java, *args)
} catch (t: Throwable) {
println(t.message);
t.printStackTrace();
}
}
}
Code language: Kotlin (kotlin)
On newer Spring Boot applications this may be different:
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
try {
runApplication<DemoApplication>(*args)
} catch (t: Throwable) {
println(t.message);
t.printStackTrace();
}
}
Code language: Kotlin (kotlin)
After doing this, I got a much more helpful error message:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someCustomService' defined in file
Code language: Shell Session (shell)
At this point I’m still stuck, but I know I’m stuck because of some config. So I’m a lot further on in the debugging process.
Anyway, I hope this little code snippet helps you. I really have no idea how basic a fix this is, but I thought it wouldn’t harm anyone to share it.
I’ve been stuck for hours. Thank you so much for this post!!!