A developer has created a tool that bridges a significant compatibility gap in Java development: enabling Java 8 environments to execute code compiled for Java 9. The Java 9 to Java 8 Desugarer operates at the bytecode level, transforming JAR files compiled with Java 9 into fully backward-compatible Java 8 packages complete with a bundled runtime library.

Core Functionality

The desugarer achieves compatibility through intelligent bytecode transformation rather than source-code recompilation. It modifies compiled Java classes to downgrade their internal version markers from Java 9 (version 53) to Java 8 (version 52), while simultaneously handling dozens of Java 9 API additions that don't exist in Java 8.

When Java 9 introduces features that Java 8 cannot natively support—such as private methods within interfaces—the tool converts them to package-private equivalents that pass Java 8's verification process. For string concatenation operations, it replaces Java 9's dynamic invoke approach with traditional StringBuilder bytecode sequences.

Handling Modern APIs

The real complexity emerges in managing new Java 9 library methods. Rather than failing on unsupported API calls, the desugarer intercepts references to new features and redirects them to a companion library called j9compat. This backport library provides Java 8-compatible implementations of virtually every Java 9 addition:

The tool handles expansions across multiple standard library families. For Collections, it remaps factory methods like List.of() and Map.ofEntries(). Stream processing gains support for takeWhile(), dropWhile(), and expanded iterate() variants across all stream types. Optional objects receive methods like or() and ifPresentOrElse(). Advanced features including VarHandle, StackWalker, ProcessHandle, and the Flow reactive framework all receive custom Java 8 implementations.

Technical Architecture

The implementation leverages the ASM bytecode manipulation library to parse and rewrite compiled classes. The main desugarer processes JAR files through a pipeline of specialized visitors: one handles version downgrades and interface methods, another tracks class hierarchies for proper remapping, and a third intercepts and redirects method calls to backport implementations.

The project targets practical deployment scenarios, specifically supporting Eclipse Temurin (Adoptium) Java 8 runtimes while enabling Android developers using versions 11 through 15 (API levels 30–34) to incorporate Java 9-compiled code into their applications.

Source: Hacker News Show HN