Kotlin is a general purpose programming language developed by JetBrains, known as developers of many popular integrated development environment (IDE) software. The main highlights of Kotlin are its full interoperability with Java, statically typed syntax, strong emphasis on nullability checks to avoid NullPointerExceptions, and less code verbosity than Java. Google recently announced that Kotlin is now the preferred language for developing Android apps and full support for it was added to the Android Studio IDE.
Hello World in Kotlin
Below is a basic hello world example in Kotlin giving you some basic idea about its syntax.
All Kotlin files must end in the “.kt” extension. Building a Kotlin file converts a “.kt” file in a “.class” file using the following pattern: “hello.kt” is automatically converted into a “HelloKt.class” file.
Installing Kotlin in Ubuntu Using Snap Package
The simplest and easiest way to install Kotlin in Ubuntu is to use the official Kotlin snap package. To install Kotlin from snap store, run the following command:
Manually Installing Kotlin in Ubuntu
If you don’t like installing snap packages, you can manually install Kotlin in Ubuntu or any other Linux distribution using SDKMAN. Run the following two commands in succession to do so:
$ sdk install kotlin
Verifying the Installation of the Kotlin Compiler
You can run Kotlin REPL shell to confirm successful installation. Run the command below to view the shell:
You can run any valid Kotlin code in the interactive shell shown above.
Compiling Kt File into a Jar File and Running it Using Java
To build a Kotlin file into a jar file that can be run by Java runtime, use a command in the following format:
The “-include-runtime” switch ensures that Kotlin runtime library is bundled into the jar file. Once the build process finishes, you can run the file using the following command as the template:
Running Kt File Without Building a Jar File
You can also run a “.kt” file directly without using Java runtime. To do so, first compile “.kt” file in a “.class” file using the command below:
Now you can run the file using the following command (without “.class” extension):
As stated earlier, “hello.kt” file is compiled into a “HelloKt.class” file when kotlin compiler is run.
Using a Third Party Jar Library with Kotlin
Importing a third party jar library in a Kotlin file is not enough to include it in the final generated build. You have to manually add it to the build command. Let’s assume a jar file is stored in the “lib” folder residing in the same directory as that of “hello.kt” file. Then you have to run a command using following template:
You have to replace “libs/commons-text-1.7.jar” with the path to your own jar file.
Using Multiple Third Party Jar Libraries with Kotlin
The process of using multiple libraries is the same as above, with a small difference that you have to separate library paths with a : (colon). Unfortunately I couldn’t get wildcards to work in my testing and it seems support for it is missing, so for the time being, you may have to specify full path to each and every third party library in the build command itself.
-include-runtime -d hello.jar
This marks the end of this article. Adoption of Kotlin took off after Google announced its inclusion in Android Studio as the preferred programming language. Even though Kotlin is mostly seen in Android apps today, there is no lack of third party libraries and projects to get you started. From UI libraries to web frameworks, many open source Kotlin projects are usable and are actively under development.