How to make standalone kotlin apps with gradle?


#1

I tried to make a simple “Hello World” kotlin app, with all dependencies stored within it.
Heres the code:

package com.neel.helloworld
fun main(args: Array) {
var str: String = "Hello world"
var name: String = "Neel"
str = str + " from " + name
println(str)
println(“whats up?”)
}

This is my gradle script:

buildscript {
ext.kotlin_version = '1.1.51’
repositories {
mavenCentral()
}
dependencies {
classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”
}
}
apply plugin: 'kotlin’
apply plugin: 'application’
mainClassName = 'com.neel.helloworld.HelloKt’
defaultTasks 'run’
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.11’
testCompile “org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version”
}
jar {
manifest {
attributes ‘Main-Class’: ‘com.neel.helloworld.HelloKt’
}
//NEWLINE
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

What i get is an error hen i try to run the resultant jar file which is at /projectroot/build/lib/. The error is:

$java -jar HelloWorld2.jar
Error: Could not find or load main class com.neel.helloworld.HelloKt

I forgot to mention but the name of the file is Hello.kt.
This i tried to build with this command:
$gradle jar

Is my gradle file wrong,and where so? Pls help.


#2

Your Jar file is just a zip file with a different extension. Have a look at it. What class files are contained in the Jar?


#3

The most probable explanation is that you assigned package but did not place your Hello.kt into appropriate directory. JVM expects class file with name com.neel.helloworld.HelloKt to be in com/neel/helloworld directory relative to classpath root.