Chapter 20Part VI · Modern Java
The Modern Toolchain
In this chapter: the JDK tools and APIs that changed how Java programs are built, shipped and observed between Java 7 and Java 27: the standard HTTP client, a built-in web server, custom runtimes with
jlink, native installers withjpackage, Flight Recorder, Markdown documentation, and the start of ahead-of-time optimization.
Key points
- HTTP Client (Java 11) replaced
HttpURLConnection; Java 26 added HTTP/3. jwebserverandSimpleFileServer(Java 18) serve static files for testing.jdepsfinds the modules your code needs;jlink(Java 9) builds a runtime with only those;jpackage(Java 16) makes native installers.- JDK Flight Recorder became free and open source in Java 11: low-overhead production profiling.
- Markdown doc comments (
///, Java 23),@snippet(Java 18), and ahead-of-time caches (Java 24–25) round out the toolbox.
The HTTP Client (Java 11)
In Java 7, calling a web service meant HttpURLConnection: a 1990s API with no HTTP/2, awkward streams, and no async support. Most projects pulled in Apache HttpClient or OkHttp instead. Java 11 standardized a modern client (JEP 321):
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
public class HttpClientDemo {
public static void main(String[] args) throws Exception {
// A tiny local server so the example needs no internet
// (com.sun.net.httpserver: since Java 6)
HttpServer server =
HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/release", exchange -> {
byte[] body = "Java 27 shipped on 2026-09-15"
.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, body.length);
exchange.getResponseBody().write(body);
exchange.close();
});
server.start();
try {
HttpClient client = HttpClient.newHttpClient(); // Java 11
int port = server.getAddress().getPort();
URI uri = URI.create("http://127.0.0.1:" + port + "/release");
HttpRequest request = HttpRequest.newBuilder(uri).build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
} finally {
server.stop(0);
}
}
}
- Immutable builders for the client and requests, and
BodyHandlersthat turn responses into strings, files or streams. - HTTP/2 by default, with fallback to HTTP/1.1;
sendAsyncreturns aCompletableFuture. - Java 26 added HTTP/3 support (JEP 517), opted into with
HttpClient.newBuilder().version(HttpClient.Version.HTTP_3). - On virtual threads (Chapter 18), the simple blocking
sendscales well, so you rarely needsendAsync.
A web server in the JDK (Java 18)
import com.sun.net.httpserver.SimpleFileServer;
import com.sun.net.httpserver.SimpleFileServer.OutputLevel;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
public class StaticSite {
public static void main(String[] args) throws Exception {
Path root = Files.createTempDirectory("site");
Files.writeString(root.resolve("index.html"),
"<h1>A Hike from Java 7</h1>");
var server = SimpleFileServer.createFileServer( // Java 18
new InetSocketAddress("127.0.0.1", 0),
root.toAbsolutePath(), OutputLevel.NONE);
server.start();
try {
int port = server.getAddress().getPort();
var uri = URI.create("http://127.0.0.1:" + port + "/index.html");
var response = HttpClient.newHttpClient().send(
HttpRequest.newBuilder(uri).build(),
HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
} finally {
server.stop(0);
}
}
}
SimpleFileServer (JEP 408) serves a folder of files over HTTP. From a terminal, jwebserver -p 8000 does the same for the current directory: handy for previewing a site (such as this book's) or for test fixtures. It's deliberately minimal and not meant for production.
Shipping: jdeps → jlink → jpackage
In the Java 7 era you shipped a JAR and told users to install a JRE. Today an application usually brings its own trimmed runtime.
flowchart LR
A["app.jar"] --> D["jdeps<br/>which modules?"]
D --> L["jlink<br/>custom runtime<br/>(only those modules)"]
L --> P["jpackage<br/>.msi / .pkg / .deb"]
L --> C["container image"]
Step 1: find the modules you need.
That output comes from a real run on the sample above.
Step 2: build a runtime with only those modules (jlink, JEP 282, Java 9):
$ jlink --add-modules java.base,java.net.http,jdk.httpserver \
--strip-debug --no-header-files --no-man-pages \
--output runtime
$ runtime/bin/java -cp out HttpClientDemo
The result is a fraction of a full JDK's size and contains nothing your application doesn't use.
Step 3, optionally: a native installer (jpackage, JEP 392, Java 16):
jpackage must run on the target platform: build the Windows installer on Windows, the macOS package on macOS.
Observing production: JDK Flight Recorder
JDK Flight Recorder (JFR) records what the JVM and your application are doing (allocations, locks, GC pauses, I/O, method samples) with overhead low enough for production. It was a commercial Oracle feature in the Java 7 era. Java 11 open-sourced it (JEP 328).
java -XX:StartFlightRecording=duration=60s,filename=app.jfr -jar app.jar
jcmd <pid> JFR.start duration=60s filename=app.jfr # attach to a running JVM
jfr print --events jdk.GarbageCollection app.jfr # inspect from the command line
Open .jfr files in JDK Mission Control for a visual timeline. Java 27 added in-process redaction of sensitive data in recordings (JEP 536), which makes it easier to share recordings safely.
Documentation: Markdown and snippets
/// Converts between Java release numbers and years.
///
/// Since Java 10, a release ships every **March** and **September**:
///
/// | Release | Ships |
/// |---------|-------|
/// | 26 | March 2026 |
/// | 27 | September 2026 |
///
/// @see #yearOf(int)
public class MarkdownDocs {
/// Returns the year a feature release (10 or later) shipped.
///
/// @param release the feature release number, such as `27`
/// @return the year it shipped
static int yearOf(int release) {
return 2018 + (release - 10) / 2;
}
public static void main(String[] args) {
System.out.println("Java 27 shipped in " + yearOf(27));
System.out.println("Java 21 shipped in " + yearOf(21));
}
}
- Java 23 lets doc comments use Markdown with
///(JEP 467) instead of HTML inside/** … */. Tables, lists,codeand links all work, andjavadocrenders them. To older compilers,///is just a line comment, so this sample runs on any Java. - Java 18 added
{@snippet …}(JEP 413) for code examples in API docs, including examples taken from real, compiled source files.
Faster startup: the AOT cache
Java's JIT makes long-running programs fast, but every startup repeats the same class loading and warm-up. Project Leyden is tackling that:
- Java 24 introduced an ahead-of-time cache (JEP 483): run a training run once, save the loaded and linked classes, and later runs start faster.
- Java 25 made the cache easier to create (JEP 514) and let it store method profiles so the JIT warms up sooner (JEP 515).
java -XX:AOTCacheOutput=app.aot -jar app.jar # training run (Java 25)
java -XX:AOTCache=app.aot -jar app.jar # faster startups afterwards
Go deeper in the complete edition
This page is the focused edition. The complete edition of this chapter on GitHub adds the deep dives, puzzles, exercises and interview questions, with every sample ready to run.
Summary
- The JDK now covers HTTP (11, HTTP/3 in 26), a test web server (18), and documentation in Markdown (23).
jdeps,jlinkandjpackagereplace "install a JRE" with a self-contained application.- JFR (free since 11) and the AOT cache (24–25) improve production insight and startup.
Journey's end: from javac Hello.java in 2013 to a self-contained, observable, fast-starting application today. Appendix B follows with a checklist for moving older code bases onto this modern Java.