How to Use Kotlin in IntelliJ: Step-by-Step Guide
To use
Kotlin in IntelliJ IDEA, create a new Kotlin project by selecting File > New > Project and choosing Kotlin as the language. Then write your Kotlin code in the editor and run it using the green run button or Shift + F10.Syntax
Kotlin code is written in files with the .kt extension. A simple Kotlin program starts with a fun main() function, which is the entry point. Inside the main function, you can write statements like println() to print text.
Example parts:
fun: declares a functionmain(): the main function name with parenthesesprintln(): prints text to the console
kotlin
fun main() {
println("Hello, Kotlin in IntelliJ!")
}Example
This example shows a complete Kotlin program you can write and run in IntelliJ. It prints a greeting message to the console.
kotlin
fun main() {
println("Welcome to Kotlin in IntelliJ IDEA!")
}Output
Welcome to Kotlin in IntelliJ IDEA!
Common Pitfalls
Common mistakes when using Kotlin in IntelliJ include:
- Not selecting Kotlin when creating the project, which causes missing Kotlin support.
- Forgetting to add a
mainfunction, so the program has no starting point. - Trying to run code without building the project first.
Always ensure your project SDK is set and Kotlin plugin is enabled in IntelliJ.
kotlin
/* Wrong: No main function */ // println("Hello") // This alone won't run /* Right: With main function */ fun main() { println("Hello") }
Quick Reference
Tips for using Kotlin in IntelliJ:
- Use File > New > Project and select Kotlin.
- Write code inside
fun main()to run it. - Run your program with the green run button or
Shift + F10. - Check the Kotlin plugin is enabled via Settings > Plugins.
- Use the built-in terminal and debugger for testing and fixing code.
Key Takeaways
Create a new Kotlin project in IntelliJ by selecting Kotlin as the language.
Write your Kotlin code inside a main function to run it.
Use the green run button or Shift + F10 to execute your Kotlin program.
Ensure the Kotlin plugin is enabled and the project SDK is set.
Common errors include missing main function and not selecting Kotlin at project creation.