1

I'm very new to Kotlin and Ktor and Gradle, wanted to try Ktor, so gone through the steps explained here, and ended up with this code, and structure shown in the screenshot:

As seen there are lots of error, how to fix them?

package blog

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("My Example Blog", ContentType.Text.Html)
            }
        }
    }.start(wait = true)
}

The build.gradle file is auto generated as:

group 'Example'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.4-3'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

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

enter image description here

Opal
  • 81,889
  • 28
  • 189
  • 210
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

2 Answers2

2

You have incomplete build.gradle script (missing dependencies) - see here for details. Here's the good one:

group 'Example'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.4-3'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8
ext.ktor_version = '0.4.0'

repositories {
    mavenCentral()
    maven { url  "http://dl.bintray.com/kotlin/ktor" }
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile "org.jetbrains.ktor:ktor-core:$ktor_version"
    compile "org.jetbrains.ktor:ktor-netty:$ktor_version"
    compile "ch.qos.logback:logback-classic:1.2.1"
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
kotlin {
    experimental {
        coroutines "enable"
    }
}
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
Opal
  • 81,889
  • 28
  • 189
  • 210
  • I got this error at running the app: "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation" – Hasan A Yousef Sep 22 '17 at 12:12
  • @HasanAYousef, this is rather not an error but a warning. You need to configure the logger as well. – Opal Sep 22 '17 at 12:23
  • Thanks @OPal, can you help me with the second step I want to do, in this question: https://stackoverflow.com/questions/46365605/how-to-create-jar-create-executable-of-the-ktor-embedded-server – Hasan A Yousef Sep 22 '17 at 13:10
0

I would recommend using the IntelliJ Ktor plugin to bootstrap your app. The ./gradlew run script runs right out of the box with this configuration without you needing to fiddle with the Gradle configuration. It's the easiest way to get started.

Here is an example Ktor app that uses the configuration: https://gitlab.com/tinacious/ktor-example

If you'd like to run your application from IntelliJ, check out this answer for the run configuration I use: https://stackoverflow.com/a/65350680/1870884

Tina
  • 1,186
  • 10
  • 11