CS349 User Interfaces
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Creating a Kotlin Project

  1. New Project Wizard
  2. Run Program (via IntelliJ)
  3. Run Program (via Command Line)
  4. Adding Classes (and Other Artifacts)

The instructions below help you set up a Kotlin / Gradle project in IntelliJ IDEA. If you wish to run sample code instead, see Running Samples.

Gradle is a build tool that helps manage dependencies for complex projects. We’ll let IntelliJ create a compatible Gradle project for all of our projects. For those new to Gradle, here’s a Gradle walkthrough video that discusses the reasons we use Gradle, and how it works (and here’s the slides).

The following steps will set up a Kotlin command-line project that uses Gradle.

New Project Wizard

Launch IntelliJ IDEA. From the Welcome screen, select New Project to be taken to the project wizard. From here, you can choose one of many different types of project to create.

To create a Kotlin command-line application, choose New Project as your project type and fill in these values:

  • Name: Pick a project name (HelloWorld in the example below)
  • Location: Choose a directory to save the project.
  • Language: Kotlin
  • Build System: Gradle
  • JDK: “temurin-17”, or “Download JDK…” if you have not installed a JDK yet.
  • Gradle DSL: Kotlin

welcome

Click Create to create your new project.

IntelliJ should open the main project view, showing the project files on the left hand side and code in the middle. We have also clicked on the Gradle button on the right-hand side to show the gradle tasks. Depending on your computer and network connection, this might take a few minutes.

Main window

Run program (via IntelliJ)

In the Gradle tasks window, double-click on Tasks/application/run to build and run your application. You will see the following output in the Run-window:

Run window

Run program (via Command Line)

Alternatively to running your program from within IntelliJ, you may also (compile and) run your program from the command line.

macOS

On macOS, all you have to do is opening a Terminal window, navigating to your project root folder (by default in ~/IdeaProjects/) and execute Gradle with the desired script, for example, ./gradlew run for compiling and running your program.

Run from Terminal (macOS)

Windows

On Windows, the overall procedure is the same, i.e., executing gradlew.bat run in the project root folder. Before doing so, however, you need to set up your JAVA_HOME-environmental variable. You can do that either via System Properties -> Environment Variables, which is persistent, or in the command prompt via set JAVA_HOME=..., which is not persistent. IntelliJ installs all JDKs in C:\Users\[your user name]\.jdks\[your JDK version], for example, C:\Users\areetz\.jdks\temurin-17.0.5.

Run from Command Prompt (Windows)

Congratulations, you are done! You can use this as a starting point for exploring Kotlin! The only other thing we want to point out is how to add classes (and other artifacts) to your project.

Adding Classes (and Other Artifacts)

To add a new class (or other artifact) to your project, right-click on the desired folder and select New\Kotlin Class/File (or New\Package for packages).

Add new class

In the next window, you must choose a name and a type (e.g., Class, Interface, or Data class) for the new artifact. In the example below, we created a new Class of name Pos3D and used it in the project’s Main file.

Use new class