This step-by-step guide walks you through creating and running your first Kotlin program using IntelliJ IDEA, the official recommended IDE for Kotlin development.
Prerequisites
- IntelliJ IDEA installed (Community or Ultimate Edition)
- JDK 8 or later installed and configured
- Basic familiarity with IDE navigation
Step 1: Launch IntelliJ IDEA
- Open IntelliJ IDEA
- You'll see the Welcome Screen with several options
- Click New Project
- The New Project wizard opens
- Go to File → New → Project
- The New Project wizard opens
- In the left sidebar, select Kotlin
- Choose Kotlin/JVM (for console applications)
- Click Next
Step 2: Create New Project
Option A: From Welcome Screen
Option B: From Menu Bar
Step 3: Configure Project Settings
Select Project Type
Project Configuration
Name:
HelloKotlin
Location: Choose where to save the project:
C:\Users\YourName\IdeaProjects\HelloKotlin
Build System: Select one:
- Gradle (Kotlin DSL) - Recommended for larger projects
- Maven - Alternative build tool
- IntelliJ - Simple, no build tool (good for learning)
JDK: Select your installed JDK:
- Choose from dropdown
- Or click Download JDK to install automatically
Example configuration:
Name: HelloKotlin
Location: C:\Users\YourName\IdeaProjects\HelloKotlin
Build System: IntelliJ
JDK: 17 (corretto-17)
Click Create
Step 4: Understanding Project Structure
IntelliJ creates this structure:
HelloKotlin/
├── .idea/ # IDE configuration
├── out/ # Compiled output
├── src/ # Source code
│ └── Main.kt # Your Kotlin files go here
└── HelloKotlin.iml # Project module file
Step 5: Create Kotlin File
Method 1: Quick Creation
- Right-click on
srcfolder in Project view (left sidebar) - Select New → Kotlin Class/File
- In the dialog:
- Choose File (not Class)
- Enter name:
HelloWorld - Press Enter
Method 2: Using Menu
- Click on
srcfolder - Go to File → New → Kotlin Class/File
- Follow same steps as Method 1
Result
A new file HelloWorld.kt is created and opened in the editor.
Step 6: Write Your First Kotlin Code
In HelloWorld.kt, type:
fun main() {
println("Hello, Kotlin!")
println("Welcome to IntelliJ IDEA!")
}
Code Explanation
-
fun main: Entry point function -
println: Prints text with newline - Strings in double quotes
- No semicolons needed
IntelliJ Features While Typing
Auto-completion:
- Start typing
fun- IntelliJ suggestsfun - Press
Tabto complete
Auto-closing:
- Type
(- IntelliJ adds)automatically - Type
{- IntelliJ adds}automatically
Syntax highlighting:
- Keywords appear in color
- Strings appear in different color
- Errors underlined in red
Step 7: Run the Program
Method 1: Green Play Button
- Look for the green ▶ (play) icon next to
fun main - Click the play button
- Select Run 'HelloWorldKt'
- Right-click anywhere in the editor
- Select Run 'HelloWorldKt'
Method 2: Context Menu
Method 3: Keyboard Shortcut
Windows/Linux:
Ctrl + Shift + F10
macOS:
Control + Shift + R
Method 4: Top Menu Bar
- Click Run in menu bar
- Select Run 'HelloWorldKt'
Step 8: View Output
Run Tool Window
The Run window opens at the bottom showing:
Hello, Kotlin!
Welcome to IntelliJ IDEA!
Process finished with exit code 0
Understanding the Output
- First two lines: Your program output
-
exit code 0: Program ran successfully -
exit code != 0: Program had an error
Step 9: Modify and Re-run
Let's make the program interactive:
fun main() {
println("=" .repeat(40))
println("Welcome to Kotlin Programming!")
println("=" .repeat(40))
val language = "Kotlin"
val year = 2024
println("\nLanguage: $language")
println("Year: $year")
println("$language is awesome!")
}
Run again to see the new output:
========================================
Welcome to Kotlin Programming!
========================================
Language: Kotlin
Year: 2024
Kotlin is awesome!
Process finished with exit code 0
Useful IntelliJ IDEA Features
1. Code Formatting
Auto-format your code:
Windows/Linux: Ctrl + Alt + L
macOS: Cmd + Option + L
2. Code Completion
Type pri and press Ctrl + Space to see suggestions:
-
println -
print -
private
Select with arrow keys and press Enter.
3. Quick Documentation
Hover over println and press Ctrl + Q (Windows/Linux) or F1 (macOS) to see documentation.
4. Rename Refactoring
To rename a variable:
- Right-click variable name
- Select Refactor → Rename
- Or press
Shift + F6 - Type new name
- Press
Enter(renames everywhere)
5. Run Configuration
After first run, you can:
- Click play button in toolbar (top-right)
- Use shortcut
Shift + F10(Windows/Linux) orControl + R(macOS)
Common Issues and Solutions
Issue 1: No Run Button Visible
Cause: Kotlin plugin not enabled
Solution:
- File → Settings → Plugins
- Search "Kotlin"
- Enable if disabled
- Restart IDE
Issue 2: Cannot Find JDK
Cause: JDK not configured
Solution:
- File → Project Structure (Ctrl + Alt + Shift + S)
- Project Settings → Project
- Project SDK: Select or add JDK
- Click OK
Issue 3: Red Underline Under Code
Cause: Syntax error or missing import
Solution:
- Hover over red text to see error
- Press
Alt + Enterfor quick fixes - Select suggested fix
Issue 4: Program Doesn't Run
Cause: Wrong file selected or no main function
Solution:
- Ensure file contains
fun main - Right-click on correct file
- Select Run
Practice Exercises
Exercise 1: Personal Info
Create a program that prints:
Name: [Your Name]
Age: [Your Age]
Location: [Your City]
Exercise 2: Simple Calculator
fun main() {
val a = 10
val b = 5
println("$a + $b = ${a + b}")
println("$a - $b = ${a - b}")
println("$a * $b = ${a * b}")
println("$a / $b = ${a / b}")
}
Exercise 3: Multiple Lines
Print a simple pattern:
fun main() {
println("*****")
println("* *")
println("* *")
println("*****")
}
Keyboard Shortcuts Cheat Sheet
| Action | Windows/Linux | macOS |
|---|---|---|
| Run program | Ctrl + Shift + F10 | Control + Shift + R |
| Run (after config) | Shift + F10 | Control + R |
| Code completion | Ctrl + Space | Control + Space |
| Format code | Ctrl + Alt + L | Cmd + Option + L |
| Rename | Shift + F6 | Shift + F6 |
| Quick fix | Alt + Enter | Option + Enter |
Find |
Ctrl + F | Cmd + F |
| Duplicate line | Ctrl + D | Cmd + D |
| Delete line | Ctrl + Y | Cmd + Delete |
Next Steps
- Experiment with different print statements
- Try using variables (
valandvar) - Learn about Kotlin data types
- Explore IntelliJ debugging features
- Create more complex programs
Tips for Beginners
- Use code completion: Saves time and reduces typos
- Read error messages: They tell you exactly what's wrong
- Experiment freely: Can't break anything, easy to undo
- Use shortcuts: Much faster than mouse clicking
- Save often:
Ctrl + S(though IntelliJ auto-saves)
Congratulations! You've created and run your first Kotlin program in IntelliJ IDEA. Keep practicing and exploring Kotlin's features!