Skip to content

Lesson 8 of 9 · 29m

Modernizing Legacy Java Without Rewriting the Business

Use compiler upgrades, dependency analysis, characterization tests, and small idiomatic replacements to move pre-8 Java into a maintainable Java 21+ shape.

The successful Java migrations I have seen all looked boring from the outside. Nobody rewrote the order system in six weeks. They upgraded the compiler, fixed illegal access warnings, added tests around ugly edges, replaced date handling one boundary at a time, and kept shipping. The failed migrations usually began with 'while we are in here' and ended as two incompatible systems.

Establish the current truth first

Before editing, record what the application really uses:

java -version
jdeps --jdk-internals target/app.jar
jdeps --summary target/app.jar
mvn -q dependency:tree -Dverbose

jdeps reveals static dependencies and illegal JDK-internal usage. Dependency trees reveal duplicate versions, ancient libraries, and frameworks that will constrain the upgrade. A migration plan built from repository filenames is fiction; build artifacts tell the truth.

Write characterization tests around seams

Legacy code rarely has clean unit-test seams. Capture current behavior at the outer boundary: HTTP responses, batch outputs, database rows, files, or message payloads. The point is not that current behavior is correct. The point is that you can detect accidental change.

@Test
void invoiceExportMatchesCapturedShape() throws Exception {
    String output = exporter.export(sampleInvoice());
    JSONAssert.assertEquals(loadFixture("invoice-export.json"), output, false);
}

Non-strict comparison can help when ordering is unstable, but do not use looseness as anesthesia. Stabilize nondeterministic timestamps, IDs, and ordering where possible.

Upgrade the runtime before modernizing style

Moving from Java 8 to 21 is mostly an operational migration before it is a language migration: removed APIs, stronger encapsulation, TLS defaults, garbage collection changes, container awareness, and library compatibility. Deploy the newer runtime with minimal source changes first. Only then introduce records, switch expressions, and virtual threads where they pay for themselves.

Streams are not a purity test

A stream is useful when transformation, filtering, grouping, or reduction is the real operation:

Map<UUID, BigDecimal> totalsByCustomer = invoices.stream()
    .collect(Collectors.groupingBy(Invoice::customerId,
        Collectors.reducing(BigDecimal.ZERO, Invoice::total, BigDecimal::add)));

A loop is often clearer when mutation, exceptions, early exit, or operational steps dominate. Modern Java is not 'everything is a stream.' It is choosing the abstraction that survives the next production incident.

Migrate risky subsystems deliberately

Dates, concurrency, serialization, security providers, reflection, classloading, and file encoding deserve focused work. Add explicit UTF-8, test time-zone boundaries, replace synchronized shared state carefully, and treat deprecated warnings as leads rather than noise. Run production-like traffic through the new runtime before flipping all users.

Field notes

  • Do not mix runtime migration, framework upgrade, and architecture rewrite in one unreviewable change.
  • Compile with warnings enabled and read them; illegal reflective access warnings were early migration signals for a reason.
  • Pin the build JDK and runtime JDK explicitly in CI; developer laptops are not evidence.
  • Keep behavior-capture fixtures small enough to review but complete enough to expose ordering differences.
  • The best modernization PR often deletes code because the JDK now provides the behavior directly.

Was this lesson helpful?

Sign in to rate