Reputation: 450
I'm using simple text editors to write my code in Kotlin such as this:
fun main() {
var sc = Scanner(System.`in`)
var a = sc.nextInt()
var b = sc.nextInt()
print(a + b)
}
And I've tried simple compiling and running the code in command line by
kotlinc hello.kt -include-runtime -d hello.jar
followed by
java -jar hello.jar
But I don't know why recently the time to compile even the simplest of code is taking more than 5 seconds and so I decided to use Gradle for compiling and running my project. It usually takes less time to compile and run but I cannot use it for running when I want to input some values. I've tried looking for some solution but the best I could find was to give some signal to Gradle that you want to feed some input during the build but that meant chaning the build everytime for different code. So I was wondering whether I could configure my build.gradle
so that it could give me jar executable file which I could run.
Here's my build.gradle file :
buildscript {
repositories {
mavenCentral()
gradlePluginPortal()
maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4-M1"
}
}
plugins {
id 'application'
}
apply plugin: "kotlin"
repositories {
maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
group 'sangeet'
version '1.0'
mainClassName = 'MainKt'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
sourceSets {
main.java.srcDirs += './'
}
defaultTasks 'run'
run {
if (project.hasProperty('main')) {
main(project.main.replace(".kt", "Kt").capitalize())
}
}
Upvotes: 2
Views: 1975
Reputation: 33491
You'll need to include all the dependencies of your projects (e.g. kotlin-stdlib
) in a JAR file so it can be used standalone. Gradle Shadow Plugin is used for that. Just add it to your project, like:
plugins {
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
and the shadowJar
will produce a fat jar with everything you need.
Upvotes: 3