CoursesJVM internals for Kotlin and Android developers

Puzzle 0 · 1 of 5

Which program did you actually run?

Follow a source edit into the file the runtime reads.

All puzzles in this course

The problem

A class file stores a compiled type and its instructions. You change a constant from one to two. The next run still prints the old answer. Which file did that run consume?

See it

Which program did you actually run? Source Compile Class file Run

Scroll sideways to see the full diagram.

  1. The source adds one.

    Step 1 of 4
  2. The compiler translates the method.

    Step 2 of 4
  3. The class file contains the translated method.

    Step 3 of 4
  4. The launcher loads the class from out.

    Step 4 of 4

The building block

A source file is the text you edit. The Java compiler reads that text and writes a class file containing instructions. The Java Virtual Machine (JVM) can execute those instructions. In this exercise the launcher looks for Bookmark in a directory named out. Separate the two operations on paper. Under compile, write the input and output filenames. Under launch, write the name of the program and the directory searched for its classes.

javac -d out Bookmark.java
java -cp out Bookmark
javap -classpath out -c Bookmark

The first command places the compiled classes in the directory named out. The second looks there for Bookmark and starts its main method. The third prints a readable listing of the instructions in the class. Find the helper named increment. You can inspect a compiled class even if the editor currently shows different source.

ActionAddition in the editorAddition in outFirst printed line
Compile and launch the originaladd oneadd one7 8
Edit only, then launch the existing classadd twoadd one7 8
Compile the edit into out and launch againadd twoadd two7 9

Imagine you compile the fix into another directory called next, then launch using the same command as before. The build succeeds. The result still comes from out. To investigate a stale deployment, identify the artifact the launcher selected and inspect that artifact's instructions. A screenshot of the corrected source cannot establish what ran.

Try it

Run the browser experiment

Optional local lab

Complete experiment 1 in the JVM source files. Record your prediction before running the program.

Check yourself

You edit the source and run the existing class. What does its first line show?

Right. The compiler writes instructions into a class file. Running an existing class reads that earlier output.

Not quite. The launch command loads the class that already exists. Re-read that part ↑

You compiled the edit into next but still launch from out. Which result appears?

Not quite. The launch command still names out. Re-read that part ↑

Right. Compilation can succeed while the running program uses an older artifact.

Which evidence distinguishes stale compiled output from a source edit?

Right. Inspect the actual artifact selected at launch.

Not quite. The editor shows the intended input to a future compilation. Re-read that part ↑

Sources

What you now know

The compiler writes instructions into a class file. Running an existing class reads that earlier output.