Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 01 / Stage 1

How Kotlin runs

Goal: Confirm the Kotlin toolchain works on your machine, compile and run your first program two different ways, and understand what kotlinc, kotlin and the JVM are each actually doing. Time: ~3 hours · Level: comfortable in another language, new to Kotlin

What you will learn

  • What Kotlin is, and the four pieces involved in running it: the JDK, kotlinc, kotlin, and kotlin-stdlib.jar
  • Compiling with kotlinc and running the result with java -jar
  • The Kotlin script runner: running a .kts file with no separate compile step
  • kotlinc versus kotlin: a compiler and a launcher, not two names for the same thing
  • What happens between your source file and the JVM: bytecode, and the FileNameKt class every top-level fun main() compiles into
  • The classpath, and why some Kotlin programs need kotlin-stdlib.jar on it and others don't
  • Your first build: Hello.kt and a welcome banner

Topics in this part

Practice

Try each one yourself first. Then check the solution.

Every exercise is one file with a top-level fun main() (compiled normally) or, where noted, a .kts script with no main at all — no variables, no loops, no if, no classes of your own. Those come in later parts.


Exercise 1: My intro card

Scenario: On the first day of a Kotlin cohort in Hyderabad, everyone prints a small intro card — and this time, as a quick script, with no compile step of your own. What to do: Write a .kts file that prints your name, city and goal on three lines, with a line of - above and below. Run it with kotlin yourfile.kts.

Sample output:

------------------------------
Name : Kavya Reddy
City : Hyderabad
Goal : Become a Kotlin developer
------------------------------

💡 Hint: There is no string-repeat operator yet (.repeat() is a String method, Part 2) — just type the 30 dashes out once, by hand, same as the Java course's version of this exercise.


Exercise 2: Fix the broken program

Scenario: A friend sends you this file. It does not compile.

main() {
    println("Good morning")
    Println("Have a nice day")
    println("Bye")
}

What to do: Fix every error so it compiles and prints three lines.

Sample output:

Good morning
Have a nice day
Bye

💡 Hint: There are two separate bugs: the function is missing its fun keyword (without it, main() { ... } is not a valid top-level declaration at all), and Println has a capital P — Kotlin is case-sensitive, and only println (lower case) is the real function.


Exercise 3: Ticket counter banner

Scenario: Pune Junction's ticket window wants a small screen banner. What to do: Compile and run a program (kotlinc ... -include-runtime -d ... ; java -jar ...) that prints a banner, 28 characters wide, with a border of * and two lines of text inside it.

Sample output:

****************************
*   Pune Junction - PNR    *
*   Window 3, Open 24x7    *
****************************

💡 Hint: Count characters by hand so both * borders line up under the top and bottom border.


Exercise 4: Compile to one folder, run from another

Scenario: You want to keep compiled .class files out of your source folder, the way real projects do. What to do: Write FactFile.kt that prints Fact: 7 x 8 = 56 using one println with + to join text and (7 * 8). Then:

  1. Compile it straight into a build folder: kotlinc FactFile.kt -d /tmp/kt-ex4-out.
  2. Run it from there with the kotlin launcher, without moving your source file: kotlin -cp /tmp/kt-ex4-out FactFileKt.

Sample output:

$ kotlinc FactFile.kt -d /tmp/kt-ex4-out
$ kotlin -cp /tmp/kt-ex4-out FactFileKt
Fact: 7 x 8 = 56

💡 Hint: -d <folder> tells kotlinc where to put the .class file; -cp <folder> tells the launcher where to look for it. The class name is FactFileKt (section 7 of the README), not FactFile.


Exercise 5: Read your own classpath

Scenario: You want to see exactly what the JVM knows about itself under Kotlin, without guessing. What to do: Write a program that prints three lines using System.getProperty(...): java.version, java.class.path, and os.name — the exact same JDK API the sibling Java course uses, called from Kotlin.

Sample output: (your own values will differ — this is (varies))

java.version = 21.0.11
java.class.path = ex05.jar
os.name = Mac OS X

💡 Hint: System.getProperty("java.version") returns a String? in Kotlin's eyes (Part 3 explains why); join it with + the same way 03_classpath_and_stdlib.kt does, and it behaves like a plain String here.


Exercise 6: Reproduce and fix a classpath + stdlib error

Scenario: Section 8 of the README showed this happening to someone else. Now make it happen to you, on purpose, and fix it two different ways. What to do:

  1. Write ClasspathFix.kt, inside solutions/ (or wherever you keep your practice files), that builds a listOf(...) of three city names and prints .sorted() on it.
  2. Compile it there without -include-runtime: kotlinc ClasspathFix.kt -d ..
  3. Run it with plain java -cp . ClasspathFixKt. Copy the exact exception text into a comment at the top of your file.
  4. Fix it two ways and copy both successful runs into the same comment: first with kotlin -cp . ClasspathFixKt, then with java -cp ".:$(brew --prefix kotlin)/libexec/lib/kotlin-stdlib.jar" ClasspathFixKt.

Sample output: (the broken command)

$ java -cp . ClasspathFixKt
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/collections/CollectionsKt
    at ClasspathFixKt.main(ClasspathFix.kt:27)
    at ClasspathFixKt.main(ClasspathFix.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.collections.CollectionsKt

Sample output: (both fixes)

$ kotlin -cp . ClasspathFixKt
[Chennai, Delhi, Pune]
$ java -cp ".:$(brew --prefix kotlin)/libexec/lib/kotlin-stdlib.jar" ClasspathFixKt
[Chennai, Delhi, Pune]

💡 Hint: .sorted() lives in Kotlin's standard library, not the JDK, so plain java can't find it unless you put kotlin-stdlib.jar on the classpath yourself. The line number in your own exception will depend on how many comment lines sit above your fun main().


Exercise 7: Bytecode peek with javap

Scenario: You want to see, in real bytecode, what println(6 * 7) actually costs in Kotlin, and whether it folds the constant the same way the Java and Python courses showed. What to do:

  1. Write AddFacts.kt with one line: println(6 * 7) inside main.
  2. Compile it without -include-runtime, then run javap -c AddFactsKt.class.
  3. Count every instruction inside main() (not the main(String[]) overload below it), and write that count as a comment at the bottom of your file.

Sample output:

$ kotlinc AddFacts.kt -d .
$ javap -c AddFactsKt.class
Compiled from "AddFacts.kt"
public final class AddFactsKt {
  public static final void main();
    Code:
       0: bipush        42
       2: istore_0
       3: getstatic     #12                 // Field java/lang/System.out:Ljava/io/PrintStream;
       6: iload_0
       7: invokevirtual #18                 // Method java/io/PrintStream.println:(I)V
      10: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #21                 // Method main:()V
       3: return
}
Instructions in main(): 6

💡 Hint: 6 * 7 never appears in the bytecode — kotlinc already worked it out at compile time, the same constant-folding the Java and Python courses showed (bipush 42, "byte push"). Kotlin's version stores that 42 in a local variable (istore_0/iload_0) before printing it; javac's version of the same idea (Java course, Part 1) skips the local variable and pushes System.out first instead — same optimization, different compiler, different bytecode shape.


📁 About the solution filenames: exercises 4, 6 and 7 keep their lesson-mandated names (FactFile.kt, ClasspathFix.kt, AddFacts.kt) instead of exNN.kt, because for those three the filename is the subject — the compile/run commands and the generated class name both depend on it directly, exactly as in the sibling Java course's Part 1.

👉 Solutions

Show the solutions for Part 1 (7 files)

solutions/AddFacts.kt

// Exercise 7: Bytecode peek with javap
fun main() {
    println(6 * 7)
}

// Instructions in main(): 6

solutions/ClasspathFix.kt

// Exercise 6: Reproduce and fix a classpath + stdlib error
//
// Real transcript (run from the solutions/ folder, compiled WITHOUT -include-runtime):
//
//   $ kotlinc ClasspathFix.kt -d .
//   $ java -cp . ClasspathFixKt
//   Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/collections/CollectionsKt
//       at ClasspathFixKt.main(ClasspathFix.kt:27)
//       at ClasspathFixKt.main(ClasspathFix.kt)
//   Caused by: java.lang.ClassNotFoundException: kotlin.collections.CollectionsKt
//       ...
//
// plain java has no idea what kotlin.collections.CollectionsKt is: the JDK's classpath
// has the JDK's own classes, not the Kotlin standard library. .sorted() lives in that
// library, so it needs kotlin-stdlib.jar on the classpath too.
//
// Fix 1 -- let the kotlin launcher add the stdlib for you:
//
//   $ kotlin -cp . ClasspathFixKt
//   [Chennai, Delhi, Pune]
//
// Fix 2 -- add it to java's classpath yourself:
//
//   $ java -cp ".:$(brew --prefix kotlin)/libexec/lib/kotlin-stdlib.jar" ClasspathFixKt
//   [Chennai, Delhi, Pune]
fun main() {
    val cities = listOf("Pune", "Delhi", "Chennai")
    println(cities.sorted())
}

solutions/ex01.kts

// Exercise 1: My intro card
// Run: kotlin ex01.kts
println("------------------------------")
println("Name : Kavya Reddy")
println("City : Hyderabad")
println("Goal : Become a Kotlin developer")
println("------------------------------")

solutions/ex02.kt

// Exercise 2: Fix the broken program (the fixed version)
fun main() {
    println("Good morning")
    println("Have a nice day")
    println("Bye")
}

solutions/ex03.kt

// Exercise 3: Ticket counter banner
fun main() {
    println("****************************")
    println("*   Pune Junction - PNR    *")
    println("*   Window 3, Open 24x7    *")
    println("****************************")
}

solutions/ex05.kt

// Exercise 5: Read your own classpath (and a couple of other JVM properties)
fun main() {
    println("java.version = " + System.getProperty("java.version"))
    println("java.class.path = " + System.getProperty("java.class.path"))
    println("os.name = " + System.getProperty("os.name"))
}

solutions/FactFile.kt

// Exercise 4: Compile to one folder, run from another
// Commands used (run from the solutions/ folder):
//   kotlinc FactFile.kt -d /tmp/kt-ex4-out
//   kotlin -cp /tmp/kt-ex4-out FactFileKt
fun main() {
    println("Fact: " + 7 + " x " + 8 + " = " + (7 * 8))
}

Common mistakes

Mistake Why it is wrong Fix
kotlin Hello.kt kotlin doesn't compile source files, only run compiled output, jars, scripts or expressions kotlinc Hello.kt ... to compile, then run the result
kotlinc Hello.kts A .kts script is run, not compiled-then-run by hand kotlin Hello.kts
Running java -cp <dir> ClassName on code that uses a Kotlin stdlib function Plain java never adds kotlin-stdlib.jar to the classpath by itself kotlin -cp <dir> ClassName, or add kotlin-stdlib.jar to -cp yourself
Expecting a file named 06_report.kt to compile to class ReportKt A leading digit can't start an identifier, so kotlinc prefixes an underscore: _06_reportKt Either accept the underscored name, or avoid leading digits in filenames whose class name you'll reference directly
Forgetting -include-runtime and shipping a loose .jar The receiving machine needs kotlin-stdlib.jar on its own classpath, or the jar won't run alone Add -include-runtime for a jar meant to run anywhere
Reading NoClassDefFoundError: kotlin/collections/CollectionsKt as "my code is broken" It means "this class wasn't on the classpath I was given", not a bug in your logic Check whether kotlin-stdlib.jar is on the classpath you used to run it
Assuming println always needs kotlin-stdlib.jar on the classpath A plain string argument compiles straight to a JDK call, no stdlib class involved Don't assume — check with javap -c, same as section 7

Quick recap

  • Kotlin has no runtime of its own: it compiles to JVM bytecode and relies on a JDK underneath, plus its own kotlin-stdlib.jar for anything beyond plain JDK calls.
  • kotlinc file.kt -include-runtime -d out.jar compiles into a self-contained, runnable jar; java -jar out.jar runs it.
  • .kts files are scripts: kotlin file.kts compiles and runs them in one step, every time you run it.
  • kotlinc is the compiler; kotlin is the launcher, and only kotlin adds kotlin-stdlib to the classpath automatically.
  • A top-level fun main() in X.kt compiles into a class named XKt (digit-prefixed filenames get an underscore); javap -c shows the real bytecode, same tool the sibling Java course uses.
  • NoClassDefFoundError: kotlin/... on a plain java run almost always means kotlin-stdlib.jar is missing from that classpath.

Checklist

  • I confirmed kotlinc -version and java -version on my own machine.
  • I compiled Hello.kt with kotlinc -include-runtime -d hello.jar and ran it with java -jar.
  • I ran the same program again as a .kts script with kotlin, with no explicit compile step.
  • I can explain the difference between kotlinc and kotlin without looking it up.
  • I looked at real bytecode with javap -c and found the FileNameKt class myself.
  • I deliberately caused and then fixed a kotlin-stdlib classpath error.
  • I built and ran the welcome banner myself.

Search the guide