It could be one of the following things:
- The application code
- The run configuration
I am leaning towards there being a problem with the run configuration rather than the application code.
Application code
Even though I think your issue is with the run configuration, in case that it isn't, I'm providing sample code here.
When I use the IntelliJ Ktor plugin, the Ktor app is bootstrapped as follows:
Application.kt
package com.example
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@kotlin.jvm.JvmOverloads
@Suppress("unused") // Referenced in application.conf
fun Application.module(testing: Boolean = false) {
routing {
get("/") {
call.respondText("Hello, world!", ContentType.Text.Plain)
}
}
}
application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ com.example.ApplicationKt.module ]
}
}
I've provided an example Ktor code base here: https://gitlab.com/tinacious/ktor-example
Run configuration
It could be your run configuration in IntelliJ. It needs to be set up as a Kotlin script (and not an Application). When it's set up as an Application, I get the same error you do. Here is how to set up the run configuration I have, allowing me to run the server in IntelliJ:
- Add a configuration
- Choose Kotlin script from the Templates section. Do not choose Application.
- Near the bottom where it says "Use classpath of module" choose your
application.main
.
- Near the top where it says "Main class" you should be able to choose your main application. I found this only showed up after I chose the classpath of the module.
Here are the relevant sections in the configuration:

Here is what I describe in step 4, i.e. I can choose my main class after the class path is added:

The run symbol should be a Kotlin logo:

I would recommend installing the IntelliJ Ktor plugin for bootstrapping your project. It uses Gradle and bootstraps everything so when you run ./gradlew run
, you are running it properly and can access it without the manual build configuration step.