How to run your Kotlin gradle built app from the command line

So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with

gradle assemble

this outputs a a “build/libs/kotlin.jar” .jar file, but you have no clue how to run your Kotlin code from the command line.

Doing it by hand with “java -cp ” is too much work, and there is a way to do

gradle run

or even

gradle -b /home/myuser/mykotlinapp/build.gradle run

in case you need to run your Kotlin script from a cronjob.

Make sure you have the following inside your build.gradle script in order to make the “run” task available


apply plugin: 'application'

// DO notice the "Kt" suffix on the class name below, if you don't use the Kt generated class you will get errors
mainClassName = 'com.myapp.MyKotlinAppKt'

// optional: add one string per argument you want as the default JVM args
applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1g"]

What if I don’t want to use gradle, and just java

ok… ok…
java -cp $KOTLIN_LIB/kotlin-runtime.jar:build/libs/kotlin.jar: com.myapp.MyKotlinAppKt

add ssh identities to your ssh agent when you start your bash session

Put this somewhere on your .bash_profile

[bash]
function addSSHIdentities() {
pushd ~/.ssh
#add all your keys here
ssh-add some_private_key
ssh-add some_private_key_2
ssh-add some_private_key_3

ssh-add some_private_key_N
popd
}

function startSSHAgent() {
SSH_AGENT_PROCESSES=`ps aux | grep ssh-agent | grep -v grep | wc -l`

if [ $SSH_AGENT_PROCESSES -gt 0 ]
then
echo "SSH Agent was running. Not adding identities"
else
echo "Starting ssh-agent…"
ssh-agent
echo "Adding SSH Identities"
addSSHIdentities
fi
}

startSSHAgent
[/bash]