Using Kotlin with Gradle
I am new to Kotlin
and Gradle
and tried these steps, so I got the following 2 files
after running, gradle init
I changed build.gradle
as follows:
// 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 ran gradle build
and gotbuild\classes\main\hello\HelloKt.class
my question is why the file is .class
not generated .jar
and how to get the file .jar
and how to run it, i tried to run the generated file with kotlin -classpath HelloKt.class main
but got the errorerror: could not find or load main class hello.main
Thanks for @ hotkey's answer, it got me going the right way.
First of all, there is a mistake in the declaration of the main class, as it must follow the new methodology, which is in the following format:
mainClassName = '[your_namespace].[your_arctifact]Kt'
namespace = package name
arctifact = filename
so given the names given in the example above, where filename is: Hello.kt
and the namespace hello
is then:
mainClassName = `[hello].[Hello]Kt`
using the previous method which contains:
apply plugin: 'application'
mainClassName = 'hello.HelloKt'
the generated .jar file does not include the kotlin runtime, so the only way to execute it is:
d:/App/build/libs/kotlin -cp App.jar hello.HelloKt
but to create a standalone jar
that can run on its own, and contains kotlin runtime
, then build.gradle
must 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 a gradle build
file [your_working_folder].jar
will be generated in a folder build/libs
assuming the working folder name is application, then an app.jar file will be generated .
You can use one of the following two commands to run this file:
D:\App\build\libs\java -jar App.jar
OR
D:\App\build\libs\kotlin App.jar hello.HelloKt
Classes are direct output from the Kotlin compiler and must be packaged in JARs using Gradle afterwards. To create a JAR, you can run a task jar
just like in a Java project:
gradle jar
This task usually runs on time gradle build
due to task dependencies.
This will package the Kotlin classes into a JAR archive (along with other JVM classes if you have a multilingual project), usually found in .build/libs/yourProjectName.jar
Regarding running the JAR, see this Q&A for a detailed explanation: (link)