Programming Kotlin Applications. Бретт Мак-Лахлин
need advanced permissions to install the Launcher Script that IntelliJ creates if you accepted the default location on Mac OS X.
Be sure you select the Kotlin/JVM option when creating the project, as shown in Figure 1.4.
FIGURE 1.3 You'll generally be either creating a project from scratch or importing one from a code repository, like GitHub.
FIGURE 1.4 IntelliJ makes getting going in Kotlin simple and prompts you on creating a new project to include Kotlin libraries.
Create Your Kotlin Program
Once your project is up and running, create a new Kotlin file. Find the src/
folder in the left navigation pane, right-click that folder, and select New ➢ Kotlin File/Class (see Figure 1.5). You can enter the code from Listing 1.2, and it should look nice and pretty, as shown in Figure 1.6 (thanks IntelliJ!).
NOTE Your IDE may not be configured exactly like mine. If you don't see the src/
folder, you may need to click Project on the left side of your IDE to display the various folders, and possibly click again on the name of the project.
FIGURE 1.5 Kotlin code should go in the src/ folder.
NOTE From this point forward, code will typically not be shown in an IDE. That way, you can use the IDE of your choice (or the command line), because you should get the same results across IDEs.
FIGURE 1.6 IntelliJ automatically formats code and adds sensible syntax highlighting.
Compile and Run Your Kotlin Program
All that's left now is to compile and run the program yourself. This is easy, because IntelliJ gives you a convenient little green arrow to click when you have a Kotlin file with a main()
function defined. Just hover over the arrow and click (see Figure 1.7). You can then select Run and your filename (I named mine “UselessPerson”). Your program will be compiled and run, with the output shown in a new pane at the bottom of the IDE (see Figure 1.8).
FIGURE 1.7 You can click the green Run button and select the first option to build and run your code.
FIGURE 1.8 The empty output of your program (which will soon be non-empty) displays in its own window.
In this case, you shouldn't get any errors, but there's not any output either. We'll fix that shortly.
Fix Any Errors as They Appear
One last note before getting back to improving that useless Person
class. IntelliJ and all other IDEs are great at giving you visual indicators when there is a problem with your code. For example, Figure 1.9 shows IntelliJ once it's tried to compile the same program with an error. In this case, the open and close parentheses are missing from line 8. You'll see an orange indicator in the code editor and an error indicating line 8 (and column 20) in the output window.
You can then easily fix the error and rebuild.
Install Kotlin (and Use the Command Line)
For power users, there's a tendency to want to use the command line for nearly everything. Kotlin is no exception. Because it's “mostly Java” in the sense that it runs using a JVM and JDK, you can get pretty far without a lot of work.
Command-Line Kotlin on Windows
For Windows users, you'll first need a JDK. You can download one from the Oracle Java download page at www.oracle.com/technetwork/java/javase/downloads
. That download has version-specific instructions that are easy to follow.
Once you have Java, you need to get the latest Kotlin release from GitHub. You can find that at github.com/JetBrains/kotlin/releases/latest
(that link will redirect you to the latest release). Download the release and follow the instructions and you'll be good to go.
FIGURE 1.9 Good IDEs help you quickly find and fix errors.
NOTE These instructions are intentionally a bit sparse. If you're using the command line already, you probably don't need a lot of hand holding. For almost everyone else, though, using an IDE really is the best approach. As a bonus, you can also use IntelliJ as a proxy for the compiler, so you may just want to save the time it would take you to mess with the command line and put it into coding Kotlin!
Command-Line Kotlin on Mac OS X
The easiest path to getting Kotlin working on Mac OS X is to use one of the package managers popular on Macs: either Homebrew (brew.sh
) or MacPorts (www.macports.org
). Both of these make getting Kotlin up and running trivial.
For MacPorts, just run the following command:
brett $ sudo port install kotlin
This requires elevated permissions, but after it runs, you'll be all set.
For Homebrew, first do an update:
brett $ brew update
Next up, install Kotlin:
brett $ brew install kotlin