Kotlin in AWS Cloud 9

Here is a short tutorial on using Kotlin in cloud 9.

Pre-requisites

  1. Setup a cloud 9 development environment https://aws.amazon.com/cloud9/

Installing the tools

There is obviously more than one way of writing a kotlin and building a kotlin application. The basic instructions for java applications describe a good starting point; I’ll be using gradle for this tutorial, but if you want to use maven try looking here:

In the terminal at the bottom install gradle and java. In update-alternatives you need to specify the java 1.8 version

sudo yum -y install java-1.8.0-openjdk-devel
sudo update-alternatives --config java 
sudo update-alternatives --config javac
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" sdk install gradle

In the IDE create a new fille called Hello.kt in the following folder structure:

src/main/kotlin/Hello.kt

Hello.kt

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

Also build.gradle

build.gradle

buildscript {
ext.kotlin_version = '1.2.61'

repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"
}
}
plugins {
id "org.jetbrains.kotlin.jvm" version "1.2.61"
}
apply plugin: 'kotlin'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'application'
mainClassName = 'HelloKt'
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

Type the following command

gradle wrapper
./gradlew build

You should see your library in build/libs. I like to use shadowJars so change your build script to add:

classpath "com.github.jengelman.gradle.plugins:shadow:1.2.3"

To buildscript.dependencies. Also add

apply plugin: 'com.github.johnrengelman.shadow'

Below apply plugin: ‘kotlin’. Now try

./gradlew shadowJar
java -jar libs/build/<name>-all.jar

Output:

Hello, world!

Next —

Kotlin in Cloud 9 for AWS lambda