How I Fixed: Kotlin Spring Boot Auto Reload in IntelliJ IDEA

Coming from a world of JavaScript (and prior, PHP), I am used to my code automatically updating as soon as I save my project. Lately I’ve been playing around with Kotlin, and as of today, Kotlin with Spring Boot inside Intellij IDEA.

And what bugged the heck out of me was that the project needed a manual restart each and every time I made a change.

Now, I found several tutorials that explain how to fix this, but none of them actually solved my problem. So here’s what I had to do – and sadly it’s not absolutely perfect, but it is 95% of the way there.

As ever, if you know a better way, please do share by leaving a comment. Thanks!

The Stuff Everyone Else Says

Google for this problem and there’s a set of steps you will find in almost every result:

  1. Add the Spring Boot Dev Tools to your project dependencies. I am using Grade, so here’s what I had to do:
dependencies {
   implementation("org.springframework.boot:spring-boot-devtools")
   ... other stuff
}
  1. File > Settings… > Build, Execution, Deployment > Compiler > tick Build project automatically
  2. Press SHIFT+CTRL+A (Win/*nix) or Command+SHIFT+A (Mac) to open a pop-up windows, type registry
  3. Find and enabled this option compiler.automake.allow.when.app.running

So far, so good. All those steps are taken from here.

However, after doing this, my project didn’t start auto-restarting whenever I made a change.

The Extra Step

What I had to do was edit my project’s run/debug configuration:

And then in there, select to “update classes and resources” on update, and frame deactivation:

It’s slap bang in the middle, if you are missing it…

Once I’d done this, the Spring Boot app did begin to restart when I made a change to my, admittedly, very simple application.

I had this:

package demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
   runApplication<DemoApplication>(*args)
}

data class Message(val id: String?, val text: String)

@RestController
class MessageResource {
   @GetMapping
   fun index(): List<Message> = listOf(
         Message("1", "Hello"),
         Message("2", "Dag"),
         Message("3", "Goedenavond"),
   )

}

And then I could add a fourth, fifth, sixth, etc entry to the list:

@GetMapping
fun index(): List<Message> = listOf(
      Message("1", "Hello"),
      Message("2", "Dag"),
      Message("3", "Goedenavond"),
      Message("4", "Chris"),
      Message("5", "Code Review Videos"),
)

And so why I say it’s not perfect is that yes, it does rebuild the project, but only when IntelliJ loses focus.

In other words, I need to manually switch to e.g. Postman and only then does the project rebuild. I find this a little strange, but it’s good enough.

My concerns would be that if you have a large Kotlin / Java project, this compilation step may take a while, and so that’s why this isn’t a thing by default(?).

Who knows. It’s fine for my “hello world” tier abilities. And also my PC is ridiculously OP anyway, so 🤷 right?

How I Fixed: cannot access ‘java.io.serializable’ which is a supertype of ‘kotlin.int’

A super easy one today, as things often are when you’re just starting out. But they can be frustrating when they happen all the same.

When learning Kotlin I’ve been using the Kotlin Koans project that’s part of IntelliJ IDEA Edu.

When filling in the examples, I have been getting the error:

cannot access 'java.io.serializable' which is a supertype of 'kotlin.int'

The fix is really simple, though a little intuitive.

sudo apt install default-jre

// after which

➜  ~ java --version
openjdk 11.0.11 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.10)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.10, mixed mode, sharing)

So, all good. I didn’t have java installed.

Makes me wonder how on earth the example Koans were able to run then, given I was at least 6 Koans in before I got so annoyed with that error that I decided to fix it.