👨🏻💻🎒 Java Runtime Architecture: From Source to Execution 🏁 What actually happens when you run a Java program? 🤔💭 Here's the end-to-end path - short, accurate, and to the point. * You write java, javac compiles to platform-independent .class bytecode designed for a JVM, not directly for your CPU/OS. * Starting the JVM, when you command-in "java -jar app.jar" the OS launches a native process (java.exe on Windows). That process loads the JVM as native code (jvm.dll on Windows) and builds a runtime in the same process: heap, stacks, class metadata (Metaspace), threads. Note: The JVM isn't a separate OS process—it lives inside the Java process and manages its own memory and threads using OS allocations. * A JAR is a ZIP. The JVM (via ClassLoaders): * Reads META-INF/MANIFEST.MF for Main-Class (this can also be used to find entry-points) * Streams class bytes/resources from the JAR (it doesn't need to extract to disk) * Defines classes in memory. To the OS it's just file I/O, only the JVM understands the JAR's structure. * Hot code runs faster via tiered compilation: * Interpreter runs bytecode first * JIT compiles hot methods to native machine code in RAM * Result: portability with near-native speed. (Some distributions also support AOT, but JIT is the default.) * Java threads are native OS threads (1:1 mapping since Java 5). * File/network I/O, timers, memory reservation, etc., use the JVM's own native code calling OS APIs. * JNI is the standard bridge for your Java code to call native libraries when needed (not a requirement for ordinary I/O). What You'll See in Task Manager/Activity Monitor? * Typically a single java/java.exe process containing: * The loaded JVM library * Multiple native threads (GC, JIT, app) * Memory regions: heap, Metaspace, thread stacks * From the OS view, it's a normal native process using CPU/RAM. LONG STORY SHORT: * java → bytecode → JVM executes it * JVM runs inside a native process; no extra OS VM process * Tiered JIT compiles hot paths to native code at runtime * Java is "cross-platform" because the same bytecode runs on any compatible JVM #Java #JVM #SoftwareEngineering
Informative
How does this change with Virtual Threads in Java 21? Do they still map 1:1 to OS threads, or does the JVM now use its own scheduler with a pool of carrier threads?