3
votes

I've created a Ktor application with Gradle and I followed the tutorial on https://ktor.io/quickstart/quickstart/gradle.html#initial.
At the end it says:

And then go to Run -> Edit Configurations select the blog.BlogAppKt configuration and change its Main class to: io.ktor.server.netty.EngineMain

Now when we run the new configuration, the application will start again.

I am searching for the option but could not find it:

enter image description here

The gradle file looks as follows:

plugins {
    application
    kotlin("jvm") version "1.3.61"
}

group = "org.example"
version = "1.0-SNAPSHOT"


val ktor_version = "1.3.0"

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    compile("io.ktor:ktor-server-netty:$ktor_version")
    compile("io.ktor:ktor-server-core:$ktor_version")
    compile("ch.qos.logback:logback-classic:1.2.3")
    testCompile(group = "junit", name = "junit", version = "4.12")
}

tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
}

application {
    mainClassName = "io.ktor.server.netty.EngineMain"
}

Update

I'v tried following configuration:

enter image description here

But as you can, there is a error message.

What do I have to add to run the Ktor app?

6
Do you have the Kotlin and Gradle plugins installed?ordonezalex
Yes. Both are installed.softshipper
Which JDK version are you using? I can reproduce your issue with OpenJDK 13, but not with OpenJDK 8. I think you should follow this issue: github.com/ktorio/ktor/issues/321ordonezalex

6 Answers

4
votes

You can manually run Main function, after that it will become available

2
votes

If it's not there, create a new one with the plus button.

You can select kotlin and then fill out the main class, etc.

1
votes

You need to create new Run configuration from template Application, not from Kotlin template. (And then fill Main class and Use classpath of module fields)

source: https://ktor.io/servers/engine.html#running-the-application-from-inside-the-ide

0
votes

It might be easier if you install the Ktor plugin on intellij. That's the first thing I do usually when starting a Ktor project.

0
votes

I believe you have encountered a known bug. See the comments starting here.

When you use JDK 9 or later, you would see this stack trace if you run that configuration:

java.lang.module.ResolutionException: Modules ktor.http.jvm and ktor.server.core export package io.ktor.http to module ktor.http.cio.jvm
    at java.base/java.lang.module.Resolver.resolveFail(Resolver.java:885)
    at java.base/java.lang.module.Resolver.failTwoSuppliers(Resolver.java:797)
    at java.base/java.lang.module.Resolver.checkExportSuppliers(Resolver.java:718)
    at java.base/java.lang.module.Resolver.finish(Resolver.java:362)
    at java.base/java.lang.module.Configuration.<init>(Configuration.java:141)
    at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:316)
    at java.base/java.lang.module.ModuleDescriptor$1.resolveAndBind(ModuleDescriptor.java:2693)
    at java.base/jdk.internal.module.ModuleBootstrap.boot(ModuleBootstrap.java:361)
    at java.base/java.lang.ClassLoader.initializeClassLoaders(ClassLoader.java:210)
    at java.base/java.lang.Thread.initialize(Thread.java:428)
    at java.base/java.lang.Thread.<init>(Thread.java:153)

If you switch to JDK 8, then you might see an error in the run configuration dialog, but the application will run.

0
votes

Gradle build is failing. Choose different JVM(download if required) in ide settings>Build, Execution, Deployment>Build Tools>Gradle::Gradle JVM. (Try JDK-14 or 15)