Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA: How to specify main class in Kotlin?

I try to build a jar with Intellij Idea with Kotlin Gradle project. Idea doesn't see my main class when I try to configure Artifact

here's my Gradle :

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.60'
    id 'application'
}
group 'org.vladdrummer'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin/'
}

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

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "com.sparkjava:spark-kotlin:1.0.0-alpha"
    implementation 'com.google.code.gson:gson:2.8.2'
    runtime 'mysql:mysql-connector-java:5.1.34'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

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

Here's structure :

structure

Here's main class :

class MovieQuizBackend {

    fun main(args: Array<String>) {
        Server()
    }
}
like image 292
Vlad Alexeev Avatar asked Dec 16 '19 22:12

Vlad Alexeev


1 Answers

In Kotlin language entry point is not a method inside the class (a class with the Kt suffix in the name is generated automatically from a file). See the documentation.

Your code inside the MovieQuizBackend.kt file needs to be changed to the following:

fun main(args : Array<String>) {
    Server()
}

Just remove the class MovieQuizBackend { and } at the end.

You can even omit args if you don't plan to pass any:

fun main() {
    Server()
}

Another option is to use @JvmStatic annotation inside the companion object:

class MovieQuizBackend {
    companion object {
        @JvmStatic fun main(args : Array<String>) {
            Server()
        }
    }
}

Note that this way the main class is named just MovieQuizBackend instead of MovieQuizBackendKt, so you will need to change it in build.gradle:

jar {
    manifest {
        attributes 'Main-Class': 'MovieQuizBackend'
    }
}
like image 156
CrazyCoder Avatar answered Sep 21 '22 21:09

CrazyCoder