The starter routine consists of a main function, a build script, and a way to compile and run the code. Most people add extra folders or complex configurations before they need them.
Initialize Gradle for a Kotlin Project
Gradle is the most common build tool for Kotlin because it handles compilation, dependencies, and running tasks with a single command.
- Open a terminal in the directory where you want the project.
- Run
gradle init --type kotlin-application. The wizard createsbuild.gradle.kts, asrc/main/kotlinfolder, and a basicMain.kt. - If you prefer a manual setup, create
build.gradle.ktswith the following content:plugins { kotlin("jvm") version "1.9.0" application } repositories { mavenCentral() } dependencies { implementation(kotlin("stdlib")) testImplementation(kotlin("test")) } application { mainClass.set("MainKt") } - Run
gradle buildto verify that Gradle can download the Kotlin plugin and compile an empty project.
After this step you have a working build script that knows where source files live and which JAR to produce.
Write the Main Function in Main.kt
The entry point for any Kotlin application is a top‑level fun main() function. You can place it in any package, but the simplest starter keeps it in the default package.
fun main() {
println("Hello, Kotlin starter routine!")
}
Save this file as src/main/kotlin/Main.kt. The println call demonstrates that the runtime is correctly linked to the standard library.
Build and Execute from the Command Line
Running from the terminal lets you see exactly what the build tool does.
- Execute
gradle run. Gradle compiles the source, creates a JAR, and launches themainfunction. - You should see the line Hello, Kotlin starter routine! printed in the console.
- If you prefer a raw
javacstyle, you can use the Kotlin compiler directly:
This produces the same output without Gradle.kotlinc src/main/kotlin/Main.kt -include-runtime -d app.jar java -jar app.jar
Both approaches confirm that the code, the compiler, and the runtime are correctly wired.
Run the Program in an IDE
Most developers start with an integrated development environment because it provides instant feedback, code completion, and a visual debugger.
- IntelliJ IDEA: Open the folder as a Gradle project. The IDE will import the
build.gradle.ktsautomatically. Click the green ▶️ run icon next to themainfunction or use the Run > Run 'MainKt' menu. - VS Code: Install the Kotlin extension, open the folder, and use the built‑in terminal to run
gradle run. The output appears in the integrated terminal. - Android Studio: The same steps as IntelliJ apply, because Android Studio shares the same platform.
When the IDE builds the project, it invokes the same Gradle tasks you ran manually, so the result is identical.
Fix the Missing Standard Library Pitfall
**Misconception:** "Kotlin compiles to Java bytecode, so I don’t need to declare any dependencies."
**Fact:** The Kotlin standard library (kotlin‑stdlib) is required at runtime. If the dependency is omitted, the program compiles but crashes with a NoClassDefFoundError when it tries to use basic functions like println.
To avoid this, always include the line implementation(kotlin("stdlib")) inside the dependencies block of build.gradle.kts. Gradle will then package the library into the runtime classpath.
If you encounter the error, open build.gradle.kts and add the missing dependency, then run gradle clean build again.
Verify the Routine Works with a Simple Test
Adding a quick unit test gives confidence that the build configuration stays functional as you expand the project.
import kotlin.test.Test
import kotlin.test.assertEquals
class MainTest {
@Test
fun testOutput() {
// Capture standard output
val output = java.io.ByteArrayOutputStream()
System.setOut(java.io.PrintStream(output))
main()
assertEquals("Hello, Kotlin starter routine!\n", output.toString())
}
}
Place this file in src/test/kotlin/MainTest.kt and run gradle test. The test should pass, confirming that both compilation and runtime are correctly configured.
Next Steps After the Starter Routine
Now that the basic workflow is reliable, you can add more files, libraries, or modules without re‑creating the build logic.
- Introduce additional packages under
src/main/kotlinfor better organization. - Use
application { mainClass.set("com.example.AppKt") }to change the entry point. - Explore Kotlin’s standard library functions, such as
apply,also, and collection utilities, to write more expressive code. - When you need external code, add it to the
dependenciesblock, for exampleimplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0").
Each new addition follows the same pattern: update build.gradle.kts, write Kotlin files, then run gradle build or use the IDE’s run command.
Quick Checklist Before You Code
- Gradle installed and
gradle init --type kotlin-applicationexecuted. kotlin("stdlib")listed independencies.- File
src/main/kotlin/Main.ktcontains afun main()entry point. - Run
gradle runand see the greeting printed. - If using an IDE, confirm the project is recognized as a Gradle project.
Following this checklist each time you start a new Kotlin project ensures a smooth, error‑free beginning.