Hasan A Yousef
Hasan A Yousef

Reputation: 24908

using Kotlin with Gradle

I'm new to Kotlin and Gradle, and tried to follow these steps, so I got the following 2 files:

after running gradle init I changed the build.gradle to be:

// set up the kotlin-gradle plugin
buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {
        mavenCentral()
    }

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

// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'application'

mainClassName = "hello.main"

// add kotlin-stdlib dependencies.
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Hello.kt:

package hello   

fun main(args: Array<String>) {   
   println("Hello World!")        
}

Then I run the gradle build and got the build\classes\main\hello\HelloKt.class

my question is: Why the file generated is .class not .jar and how to get the .jar file and how to run it, I tried running the generated file using kotlin -classpath HelloKt.class main but got an error error: could not find or load main class hello.main

Upvotes: 4

Views: 3302

Answers (2)

Hasan A Yousef
Hasan A Yousef

Reputation: 24908

Thanks for @hotkey answer, it helped me going the correct way.

First of all there is a mistake in the main class declaration, as it should follow the new methodology, that is in the below format:

mainClassName = '[your_namespace].[your_arctifact]Kt'

namespace = package name

arctifact = file name

so, considering the names given in the example above where filename is: Hello.kt, and the namespace is hello, then:

mainClassName = `[hello].[Hello]Kt`

using the previous method, that contains:

apply plugin: 'application'
mainClassName = 'hello.HelloKt'

the generated .jar file is not including the kotlin runtime, so the only way to execute it, is by:

d:/App/build/libs/kotlin -cp App.jar hello.HelloKt

but in order to generate a self contained jar that can be self-executed, and contains the kotlin runtime then the build.gradle should be written as:

// set up the kotlin-gradle plugin
buildscript {
    ext.kotlin_version = '1.1.2-2'

    repositories {
        mavenCentral()
    }

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

// apply the kotlin-gradle plugin
apply plugin: "kotlin"


// add kotlin-stdlib dependencies.
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

jar {
    manifest {
        //Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
        attributes 'Main-Class': 'hello.HelloKt'
    }

    // NEW LINE HERE !!!
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

followed by gradle build, the [your_working_folder].jar file will be generated at the build/libs folder, assuming the working folder name is app, then file app.jar will be generated.

To run this file, one of the following 2 commands can be used:

D:\App\build\libs\java -jar App.jar

OR

D:\App\build\libs\kotlin App.jar hello.HelloKt

Upvotes: 1

hotkey
hotkey

Reputation: 147901

The classes are the direct output of the Kotlin compiler, and they should be packaged into a JAR by Gradle afterwards. To build a JAR, you can run the jar task, just as you would in a Java project:

gradle jar

This task is usually run during gradle build as well, due to the task dependencies.

This will pack the Kotlin classes into a JAR archive (together with other JVM classes, if you have a multi-language project), normally located at build/libs/yourProjectName.jar.

As to running the JAR, see this Q&A for a detailed explanation: (link)

Upvotes: 2

Related Questions