1
votes
var foo : String? = null

fun main(args: Array<String>): Unit {
    foo = "Hello World"
    io.ktor.server.netty.EngineMain.main(args)
}

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    // foo is null here
}

How can I access foo in Application.module and why is this an issue to begin with?

2

2 Answers

1
votes

You cannot access such variables in your modules since Ktor loads your application (and modules ofc) in a different classloader.

When you call EngineMain.main(), it goes through a long chain of calls, and one of the steps is to create a new classloader in createApplication() method here. The implementation of createClassLoader() is here.

1
votes

You can pass arbitrary arguments in the following format: -P:<argument> where <argument> is the actual name for your argument. In the Application.module you can access them via config object:

fun Application.main() {
    println(environment.config.property("<argument>").getString())
}