I’ve been trying to get started with Kotlin this weekend. Spent most of yesterady and pretty much got nowhere. I have googled, I have read the docs and getting past Hello World is not happening.
I think the problem is every document assumes the user is well versed with Java and Gradle at least, preferrably also JIDEA. I’m an embedded SW engineer who dabbles with Python occasionally so PyCharm is familiar but everything else is not.
As far as I have been able to figure out I need a build system to add any external libraries. In this case I’d like to start with Klaxon and go on to add TornadoFX. These would allow me write a usable little application.
So what I would really like at this point is a dummy’s user guide on how to create the project and get started:
- Do I start the project according to these instructions: https://kotlinlang.org/docs/reference/using-gradle.html ?
- Do I need to manually run gradle init in the directory because it seems that the build artifacts are not created automatically, so how else are they generated? If I start a Gradle project instead of Java or Kotlin project they are generated but then JIDEA works all funky (it doesn’t check the code at all etc).
- I can get Klaxon into build.gradle but adding org.json seems completely impossible. No matter how many ways I try JIDEA (and the build system) keep telling me org.json is not recognized and Parser is not defined. So what should a correct build.gradle look like?
To put it simply, what steps are missing from GitHub - cbeust/klaxon: A JSON parser for Kotlin to make it work? This page assumes I have an all dancing, all singing system up already.
My last attempt gave me even more confusing errors:
$ gradle build
:compileKotlin
Using Kotlin incremental compilation
Compilation with Kotlin compile daemon was not successful
java.lang.Exception: sun.misc.Unsafe.invokeCleaner(java.nio.ByteBuffer)
at org.jetbrains.kotlin.daemon.common.CompileService$CallResult$Error.get(CompileService.kt:60)
....
Could not connect to kotlin daemon. Using fallback strategy.
error: no class roots are found in the JDK path: /usr/lib/jvm/java-9-openjdk-amd64
The Java exception was very long so I didn’t copy all of it here. Daemon seems to be running according to ps -ef, though.
I have now created four different projects in JIDEA so far and got nowhere. Yes, Hello World works but that’s it. The code so far looks like
import java.net.URL
import org.json.*
public class HuutoAmpMain {
companion object {
@JvmStatic fun main(args: Array<String>) {
val result = URL("https://api.huuto.net/1.1/categories").readText()
print(result)
val parser: Parser = Parser()
}
}
}
Running this in JIDEA gives me plenty of errors:
Error:(7, 8) Kotlin: Unresolved reference: java
Error:(8, 12) Kotlin: Unresolved reference: json
Error:(13, 26) Kotlin: Unresolved reference: URL
Error:(15, 25) Kotlin: Unresolved reference: Parser
Error:(15, 34) Kotlin: Unresolved reference: Parser
So it’s obviously not using build.gradle. Running gradle build on the command line gives me error above. Finally, here’s the build.gradle file:
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
jcenter()
}
apply plugin: "kotlin"
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.beust:klaxon:0.30'
compile group: 'org.json', name: 'json', version: '20141113'
testCompile 'junit:junit:4.12'
}
Any help would be appreciated because I’m just about to give up. Spending a whole weekend to get the tools setup is not a good way to spend my free time. I’d rather learn Kotlin and worry about the rest later. And there is a dozen languages that are easier to start with that interest me as well (I have tried them and gotten past this point much faster, e.g. Clojure).