Hasan A Yousef Hasan A Yousef - 6 months ago 41
Java Question

How to create .jar (Create Executable) of the Ktor embedded Server

I'm very new to Kotlin and Ktor and Gradle. Was able to create embedded server as explained in Ktor site, with the following code:


package blog

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

fun Application.module() {
install(Routing) {
get("/") {
call.respondText("My Example Blog sfs 122", ContentType.Text.Html)

fun main(args: Array<String>) {
embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()

and build.gradle:

group 'Example'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.1.4-3'

repositories {
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 {
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"

The server run fine at:

and the I can see the below files in the


How can I know create an executable file .jar of this server, so I can distribute it to the user?

enter image description here

Answer Source

What you need to do is to add the following piece of code to build.gradle file:

jar {
    baseName '<jar_name>'
    manifest {
        attributes 'Main-Class': 'blog.BlogAppKt'

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }

Then run: gradle clean jar. After it's finished navigate to: build/libs and run java -jar <jar_name>.jar.

Or run it using javaw -jar <jar_name>.jar instead of java (you might need to make sure its on the path if the JAVA_HOME is not defined well). This will run it without any console.

P.S. You can also use application plugin or shadowJar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download