Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
180: Native Image Compilation with GraalVM
You've probably heard the pitch for GraalVM Native Image: lightning-fast startup times and a tiny memory footprint. It sounds like magic because it basically turns your Scala code into a standalone binary. But here is the catch—GraalVM operates on a "Closed World Assumption." It needs to know every single class and method that will ever be touched at runtime during the compilation phase. If it can't see a path to a piece of code, it throws it away to save space.
This is where most of us hit a wall. I remember the first time I tried to move a production service to a native image; it compiled perfectly, but the moment I ran it, the whole thing exploded. Let's look at a snippet that demonstrates exactly why this happens.
case class User(name: String, age: Int)
object NativeDemo {
def main(args: Array[String]): Unit = {
// Imagine this class name comes from a config file
val className = "User"
println("Attempting to load class reflectively...")
val clazz = Class.forName(className)
println(s"Successfully loaded: ${clazz.getSimpleName}")
}
}
The Runtime Crash: ClassNotFoundException
If you run this on a standard JVM, it works flawlessly. The JVM is dynamic; it just looks at the classpath at runtime and finds the User class. However, when you compile this with native-image, you'll get a runtime crash that looks something like this:
java.lang.ClassNotFoundException: User
at java.lang.Class.forName0(Native Image)
at java.lang.Class.forName(Native Image)
at NativeDemo.main(NativeDemo.scala:7)
Why? Because GraalVM's static analysis looked at your main method and saw Class.forName(className). Since className is a variable (a string), the compiler has no way of knowing which class you're actually talking about. It looks at the User case class and thinks, "Nothing in the code ever explicitly calls a constructor or a method on User, so I'll just strip it out of the binary to keep the file size small."
Teaching GraalVM about Reflection via JSON
To fix this, we have to explicitly tell GraalVM: "Hey, I know it looks like I'm not using the User class, but I'm going to access it via reflection at runtime. Please keep it."
The most reliable way to do this is by creating a reflect-config.json file. You place this file in your META-INF/native-image directory. Here is what the configuration for our broken example needs to look like:
[ { "name": "User", "allDeclaredFields": true, "allPublicMethods": true, "allDeclaredConstructors": true } ]When you rebuild the native image, GraalVM reads this JSON and adds
Userto the "keep" list. Now, whenClass.forName("User")is called, the class is actually there in the binary, and the program succeeds. I'll be honest: manually writing these JSON files is a chore, especially for large projects with dozens of case classes. In a real-world scenario, you'd likely use the GraalVMagent, which runs your app on a JVM, watches which classes are accessed reflectively, and generates this JSON for you automatically.Handling Proxy and Resource Issues
Reflection isn't the only thing that trips people up. If you're using a library that generates proxies (like some older Spring versions or certain Scala decorators) or if you're reading a file from the classpath (like
getClass.getResourceAsStream("/config.conf")), GraalVM will likely fail you again.Just like we did with reflection, you need a
proxy-config.jsonfor dynamic proxies and aresource-config.jsonfor classpath resources. If you're loading a config file, yourresource-config.jsonwould look like this:[ { "pattern": "config.conf" } ]Without this, the file is simply not bundled into the binary, and your app will return
nullwhen trying to find the resource, leading to those dreadedNullPointerExceptions that only happen in the native build.
📋 Practical Task
Exercise: Fixing a Broken Native Config Loader
You have been handed a small Scala project that is supposed to load a system property using reflection to determine which "Environment" class to instantiate. The code works on the JVM, but crashes as a GraalVM Native Image.
The Code:
case class DevEnv(name: String)
case class ProdEnv(name: String)
object EnvLoader {
def main(args: Array[String]): Unit = {
val envType = sys.props.getOrElse("env", "DevEnv")
val clazz = Class.forName(envType)
println(s"Running in ${clazz.getSimpleName} mode")
}
}
Your Task:
- Create the
reflect-config.jsonfile required to make this application work as a native image. - Ensure that both
DevEnvandProdEnvare preserved by the static analyzer. - Specify that the public methods and constructors for these classes must be accessible.
Expected Output: Provide the exact JSON content that would go into reflect-config.json to prevent the ClassNotFoundException for both environment types.
There are no comments for now.