The First Kotlin App¶
I am assuming you already have the relevant Kotlin development environments (IDE and CLI) setup. If not, my Setup: Your Development Environments post is worth scanning first.
All set? Let's go!
Using: Kotlin CLI¶
CLI Tutorial Walkthrough
1 2 3 4
Now open your terminal of choice, change to the code directory, and let's go exploring! First we need to compile the source code using
kotlinc, the command-line Kotlin compiler.
1 2 3 4
This generates the classic JAR file familiar to Java developers. You can use the
jar command to peek into the generated archive and identify generated classes.
1 2 3 4
Now run the code using the
kotlin command with the class name identified above.
Or, if you are familiar with Java - this is a standard jar file with a defined entry point, so you can also just do this:
Under The Hood: hello.jar
If you're curious about what's in a jar file, you can unpack it using:
1 2 3 4
MANIFEST.MF file gives you all the information you need, to figure out what the entry point or
Main-Class for this archive is. This is what mine looks like - and clearly identifies
_001_helloKt as the entry point.
1 2 3 4
Using: VS Code + CLI¶
Option 1: Use VSCode + CodeRunner
F1 to get the commands selector, then select
Run Code to activate the extension. It should open up a built-in terminal and produce output like this:
1 2 3 4 5
Option 2: Use VSCode Terminal manually
If you're a Java developer, the syntax and approach will be very familiar. In fact, if you look at the output of the code runner above, you can spot the commands it uses, to execute the built/run sequence! Or refer to the Using CLI section for the standard terminal commands.
To open the built-in Terminal in VS Code, you can use the "Cmd + `" shortcut on the Mac. There might be other shortcuts available for different environments.
Using: IntelliJ IDEA¶
Next, let's explore the richer tooling of IntelliJ IDEA to create a new application (from scratch), and also to work with existing code like the sample above.
How can we create a new project?
I started with the Getting Started with IntelliJ IDEA tutorial, building a
HelloKotlinverse application using the
Console Application template and default configurations. You can also read the IntelliJ Guide for more options and details.
By default, your project uses the Gradle build system with Kotlin DSL. If you open the default boilerplate project, you will see this distribution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The main file to look at right now is the
src/main/kotlin/main.kt file which contains this code:
1 2 3
Yes - the exact code sample we used for exploring CLI usage is the default "Hello World" boilerplate code - except it declares the main function's accepted
args parameter explicitly.
Change the code to send a different message - I went with
Hello Kotlinverse! for mine - then click the
Run (green arrow) in the gutter of your editor screen to run the code. You should see something like this (some output eliminated for clarity)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
And that's it! You've validated your IDE install and compiled your first app. In later sections we'll explore the purpose and usage of the various other files and settings in this IDE.
How can we import and work with existing projects?
The short answer is _"it depends!". You can import an existing IDEA project (e.g., move project to new environment), or create it from the source in a version control system, or create it from source files in your local directory.
Read this guide for more on all these options.
What we completed here:
- verified our development environment is setup correctly
- wrote the basic "Hello, World" equivalent in Kotlin
- compiled and ran it from command-line (CLI)
- setup and used the CLI from a different editor (VS Code)
- imported and ran the code in the IntelliJ IDEA IDE
- learned to build and run a new app using IntelliJ IDEA
The "Referenced Resources" section at the top of the page lists all the resources used in this segment.