https://kotlinlang.org logo
Title
j

Joseph Magara

07/14/2021, 8:51 PM
Hi All 😒imple_smile: 👋 , I’ve been trying to deploy my ktor server to GCP Cloud Run for the last few days but I keep getting this error when the server is being started up.
Error: Could not find or load main class io.ktor.server.netty.EngineMain
Does anyone know how to fix it?
Here is my Gradle files:
buildscript {
    repositories {
        jcenter()
        mavenCentral()
        google()
        maven { url "<https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven>" }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'kotlin-kapt'
apply plugin: 'java'
apply plugin: 'org.jetbrains.kotlin.jvm'


group 'com.example'
version '0.0.1'

mainClassName = "io.ktor.server.netty.EngineMain"

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
}

sourceSets {
    main.kotlin.srcDirs = main.java.srcDirs = ['src']
    test.kotlin.srcDirs = test.java.srcDirs = ['test']
    main.resources.srcDirs = ['resources']
    test.resources.srcDirs = ['resources']
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
    google()
    maven { url '<https://kotlin.bintray.com/ktor>' }
    maven { url "<https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven>" }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

test {
    useJUnitPlatform()
}

dependencies {
  ...
}
r

rnett

07/14/2021, 9:44 PM
I don't see it anywhere in your script, but are you making a shadow/far jar or WAR, etc? If I remember Cloud Run right, you just deploy the jar file? In that case, it needs to contain all of the dependencies as well, since you aren't running via the build script.
j

Joseph Magara

07/15/2021, 2:50 AM
@rnett yeah your right. I was deploying a docker container with the jar running within it but it wasn’t a fatjar so the netty engine dependency was missing. I changed over my gradle file to use the
jengelman
fatjar gradle plugin and it solved my issue.
For future reference (for anyone who faces this issue), just add the
jengelman
fatjar gradle plugin to your gradle file (and remove the
jar
closure if you had one in your gradle file) and it should resolve the issue for you. It will create a fatjar that will contain your app and it’s dependencies. This resolves the issue.