Question
I am learning Kotlin with IntelliJ IDEA and tried to run this basic program:
fun main(args: Array<String>) {
println("lol")
}
However, the build fails with this error:
Error: (5, 5) Kotlin: Unresolved reference: println
This is my first time using IntelliJ IDEA and I have not previously worked with Java projects. What project configuration or dependency might be missing?
Short Answer
println is part of Kotlin's standard library, so it should work in every correctly configured Kotlin project. An unresolved reference usually means IntelliJ IDEA is not treating the file as part of a configured Kotlin module, the module has no Kotlin standard library dependency, or its JDK/SDK setup is incomplete. You will learn how Kotlin finds built-in functions and how to check a project's essential configuration.
Concept
Kotlin code does not run in isolation. A Kotlin source file belongs to a module (or build project), and that module needs the libraries and runtime configuration required to compile it.
println is a top-level function provided by Kotlin's standard library (kotlin-stdlib). It is normally available without an import:
println("Hello")
If the compiler reports that println is unresolved, the problem is generally not the spelling of println. Instead, it indicates that the Kotlin standard library is not available to the module compiling the file, or that the file is outside the module's recognized source directories.
For Kotlin targeting the JVM, a working setup typically includes:
- A Kotlin-enabled module or a Gradle/Maven Kotlin project.
- A configured JDK, which provides the Java runtime and compiler tools used by Kotlin/JVM.
- The Kotlin standard library on the module's compile classpath.
- The
.ktfile located in a recognized source root, such assrc/main/kotlinin a Gradle project.
These pieces are usually configured automatically when you create a Kotlin project in IntelliJ IDEA or import an existing Gradle/Maven project.
Mental Model
Think of a Kotlin source file as a recipe in a kitchen.
- Your
.ktfile is the recipe. - The module is the kitchen where the recipe is prepared.
- The JDK is the kitchen equipment needed for Kotlin/JVM work.
- Kotlin's standard library is the pantry containing common ingredients such as
println, collections, and utility functions.
Writing println("lol") asks for an ingredient from the pantry. If the kitchen has no pantry connected, IntelliJ and the Kotlin compiler cannot find it. Adding random imports does not fix an unconnected pantry; the project itself must be configured correctly.
Syntax and Examples
The usual Kotlin entry point is a top-level main function:
fun main() {
println("Hello, Kotlin!")
}
main is where a console application begins. println writes text to standard output and then adds a line break.
Older Kotlin examples may include command-line arguments:
fun main(args: Array<String>) {
println("Received ${args.size} arguments")
}
Both forms are valid. If arguments are not needed, prefer the simpler version:
fun main() {
println("lol")
}
You do not normally need this import:
import kotlin.io.println
Kotlin imports its default packages automatically. If that import also cannot be resolved, it further suggests a project dependency or module configuration problem.
Step by Step Execution
Consider this program:
fun main() {
val message = "lol"
println(message)
}
Execution proceeds as follows:
- The Kotlin runtime starts the
mainfunction. val message = "lol"creates a read-only variable namedmessagecontaining the textlol.println(message)calls Kotlin's standard-library output function.- The function writes
lolto the console and moves to the next line. mainfinishes, so the program exits.
Before these steps can happen, the compiler must resolve every referenced symbol. It must find println in the Kotlin standard library. If the library is missing from the compile classpath, compilation stops before the program can run.
Real World Use Cases
Although println is often the first Kotlin function beginners use, the same dependency-resolution idea applies throughout real software.
- Command-line tools: Print progress messages, reports, or errors to the terminal.
- Learning and debugging: Temporarily inspect values while understanding program flow.
- Build scripts: Kotlin-based Gradle scripts use Kotlin libraries and Gradle APIs that must be present on the script classpath.
- Backend services: A web service depends on libraries such as Ktor, Spring, or database drivers. Missing dependencies cause unresolved-reference errors just as a missing standard library can.
- Android applications: Android modules require the Android SDK, Kotlin plugin, and Android/Kotlin dependencies to resolve framework and Kotlin code.
For production logging, use the application's logging system rather than leaving many println calls in application code.
Real Codebase Usage
In real projects, dependencies are usually declared in a build tool rather than configured manually in IntelliJ module settings.
A minimal Kotlin/JVM Gradle project commonly declares the Kotlin plugin and standard library like this:
// build.gradle.kts
plugins {
kotlin("jvm") version "2.1.0"
application
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
}
application {
mainClass.set("MainKt")
}
Its source file would normally be placed here:
src/main/kotlin/Main.kt
fun main() {
println("Application started")
}
When Gradle is imported or refreshed, IntelliJ reads this configuration and supplies the correct compiler, standard library, and classpath. This approach is more reliable than adding JAR files manually because the configuration can be committed to version control and reproduced by other developers.
In application code, output is often guarded by conditions or replaced with structured logging:
fun processOrder(orderId: String?) {
if (orderId.isNullOrBlank()) return
println("Processing order: $orderId")
}
The early return prevents invalid input from reaching later code.
Common Mistakes
Treating this as an import problem
This is usually not fixed by adding import kotlin.io.println. Kotlin default imports already make println available.
// Usually unnecessary
import kotlin.io.println
If the standard library itself is unavailable, this import may be unresolved too. Configure the module instead.
Creating a plain Java module and adding a Kotlin file
A Java module may have a JDK but no Kotlin support or Kotlin standard-library dependency. Create a Kotlin/JVM project, add Kotlin support to the module, or use Gradle with the Kotlin JVM plugin.
No Project SDK or module SDK
Kotlin/JVM needs a JDK. In IntelliJ IDEA, check File > Project Structure and ensure the project/module uses an installed JDK, not an empty or invalid SDK selection.
Putting the file outside a source root
In a Gradle project, production Kotlin files normally belong under:
src/main/kotlin
A file in an unrelated folder may not be compiled as part of the intended module. Move it into the source directory or mark the intended directory as a Sources Root when appropriate.
Mixing IntelliJ configuration with Gradle configuration
If the project uses Gradle, edit build.gradle.kts and then refresh the Gradle project. Manual IDE-only changes can be overwritten during the next Gradle sync.
Trying to fix the issue by changing main
Both of these are valid:
fun main() { }
fun main(args: Array<String>) { }
Changing the main signature does not normally resolve a missing println; that error concerns library resolution.
Comparisons
| Situation | What it means | Typical fix |
|---|---|---|
println is unresolved | Kotlin standard library or module setup is missing | Configure Kotlin support and dependencies; verify the JDK |
| A third-party type is unresolved | A library such as Ktor or Retrofit is not on the classpath | Add the dependency in Gradle/Maven and sync |
| Only one file has errors | The file may be in the wrong folder or attached to the wrong module | Move it to the source root or check its module |
println works but the program will not start | Compilation succeeds, but run configuration or entry point may be wrong | Run the file's main function and check the run configuration |
| Approach | Best use | Notes |
|---|---|---|
| IntelliJ Kotlin project wizard | Small learning projects | Sets up Kotlin and a JDK interactively |
| Gradle Kotlin/JVM project | Most JVM applications and shared projects | Dependencies are explicit and reproducible |
| Manual JAR/module configuration | Special or legacy setups | Easier to misconfigure; avoid it as a beginner when possible |
Cheat Sheet
println("text")is a Kotlin standard-library function.- It is available through Kotlin's default imports; no import is usually needed.
- For Kotlin/JVM, configure a JDK, not just a JRE.
- A Gradle Kotlin source file usually belongs in
src/main/kotlin. - In a Gradle project, declare dependencies in
build.gradle.kts, then refresh/sync Gradle. - Use a simple entry point when no command-line arguments are needed:
fun main() {
println("Hello")
}
- If
printlnis unresolved, check project setup before changing application code. - If a library dependency is unresolved, add it to the build configuration instead of copying JAR files into random folders.
FAQ
Why is println unresolved in Kotlin?
println comes from Kotlin's standard library. The usual cause is that the file's module does not have Kotlin properly configured, does not include the standard library, has no valid JDK, or is outside a recognized source root.
Do I need to import println in Kotlin?
No. println is available through Kotlin's default imports. Adding an import is normally unnecessary.
Does Kotlin require Java?
Kotlin can target several platforms. For Kotlin/JVM projects in IntelliJ IDEA, you need a JDK because Kotlin compiles to JVM bytecode and uses JVM tooling.
Should I use fun main() or fun main(args: Array<String>)?
Use fun main() when you do not need command-line arguments. Use the version with args when your program reads arguments passed from the command line. Both are valid.
Where should Kotlin files go in a Gradle project?
Production source files usually go in src/main/kotlin. Test files usually go in src/test/kotlin.
How do I add Kotlin support to an existing IntelliJ module?
Use IntelliJ's module/project configuration to add Kotlin support, or preferably migrate/configure the module with Gradle using the Kotlin JVM plugin. Also ensure a JDK is assigned to the project/module.
Why does IntelliJ show errors after I edit build.gradle.kts?
The IDE may not have reloaded the build configuration yet. Refresh the Gradle project so IntelliJ downloads dependencies and updates its classpath.
Mini Project
Description
Create a small Kotlin command-line greeter. It demonstrates a correctly configured Kotlin entry point, use of println, command-line arguments, and an early return for missing input. This is the same basic setup that should allow standard-library functions to resolve in IntelliJ IDEA.
Goal
Build and run a Kotlin program that prints a personalized greeting when a name is provided.
Requirements
Create a Kotlin/JVM project with a valid JDK configured.
Place the source file in the project's Kotlin source directory.
Create a main function that reads the first command-line argument.
Print a usage message when no name is provided.
Print a greeting when a name is provided.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Add a Maven Repository URL in Gradle Kotlin DSL (build.gradle.kts)
Learn how to add Maven Central and a custom Maven repository URL in Gradle Kotlin DSL using build.gradle.kts syntax.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.