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

Part 01 / Topic 8 of 9

The classpath, and the Kotlin standard library on it

The classpath is the list of folders and jars where a launcher looks for classes. Section 1's table already named the catch: kotlin-stdlib.jar is needed on it the moment your code calls something Kotlin's standard library provides — collection functions like .filter/.sorted, for instance — rather than something the bare JDK already has.

println from section 4 didn't need it (the bytecode in section 7 shows why: it compiles straight to System.out.println, a JDK call). A program using listOf(...).filter { ... } is different:

fun main() {
    val prices = listOf(1760, 540, 2999, 125)
    val aboveFiveHundred = prices.filter { it > 500 }
    println("Prices above ₹500: $aboveFiveHundred")
    println("java.class.path = " + System.getProperty("java.class.path"))
}

Compile it without -include-runtime, then run it with plain java:

$ kotlinc 03_classpath_and_stdlib.kt -d out03
$ java -cp out03 _03_classpath_and_stdlibKt
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/collections/CollectionsKt
	at _03_classpath_and_stdlibKt.main(03_classpath_and_stdlib.kt:6)
	at _03_classpath_and_stdlibKt.main(03_classpath_and_stdlib.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.collections.CollectionsKt
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 2 more

.filter is implemented in kotlin.collections.CollectionsKt, a class that lives in kotlin-stdlib.jar, not the JDK — and plain java only looks on the classpath you gave it (out03, nothing else). Two fixes, both verified:

$ kotlin -cp out03 _03_classpath_and_stdlibKt
Prices above ₹500: [1760, 540, 2999]
java.class.path = file:/Users/sujeet.s/Documents/RND/learning/kotlin-step-by-step/stage-1-foundation/part-01-how-kotlin-runs/examples/out03/:file:/opt/homebrew/Cellar/kotlin/2.4.21/libexec/lib/kotlin-stdlib.jar:file:/opt/homebrew/Cellar/kotlin/2.4.21/libexec/lib/kotlin-reflect.jar

$ java -cp "out03:$(brew --prefix kotlin)/libexec/lib/kotlin-stdlib.jar" _03_classpath_and_stdlibKt
Prices above ₹500: [1760, 540, 2999]
java.class.path = out03:/opt/homebrew/opt/kotlin/libexec/lib/kotlin-stdlib.jar

(The exact folder names above are specific to this machine — (varies) on yours — but the shape of each path, and which jars show up, will match.)

The printed java.class.path line proves what each fix actually did: the kotlin launcher quietly added kotlin-stdlib.jar (and kotlin-reflect.jar) itself, while the java fix only has what was typed by hand. -include-runtime (section 4) sidesteps this whole question for a shippable .jar, by putting kotlin-stdlib's classes inside the jar itself instead of asking the launcher or the classpath to find them elsewhere.

Full program: examples/03_classpath_and_stdlib.kt.

Done here? The practice set for Part 1 — including the solutions — is on the part page.

Search the guide