The jar and the three tools
The project ships as one jar with a manifest, and the three tools of the image read the same source and the same class files. This page builds the jar, runs it, and then runs checkstyle, PMD and SpotBugs on the project as it is. The outputs are short on purpose: the rules were chosen for a project of this size, not inherited from a style guide written for another one.
The source
The file is the one from the server page, unchanged: the project ships as this file and the test class of the previous page. The commands below compile it into classes and pack that folder:
package library;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
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;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
public class Desk {
record Loan(int id, String member, String title, LocalDate due, LocalDate returned) {
State state(LocalDate today) {
if (returned != null) {
return new Returned(returned);
}
long days = ChronoUnit.DAYS.between(due, today);
if (days > 0) {
return new Overdue(days);
}
if (days >= -2) {
return new DueSoon(-days);
}
return new OnLoan(-days);
}
String toJson(LocalDate today) {
return "{" + field("id", id) + "," + field("member", member) + ","
+ field("title", title) + "," + field("due", due.toString()) + ","
+ Q + "state" + Q + ":" + stateJson(state(today)) + "}";
}
}
sealed interface State permits OnLoan, DueSoon, Overdue, Returned {
}
record OnLoan(long daysLeft) implements State {
}
record DueSoon(long daysLeft) implements State {
}
record Overdue(long daysLate) implements State {
}
record Returned(LocalDate on) implements State {
}
static final LocalDate TODAY = LocalDate.of(2026, 11, 20);
static final List<Loan> LOANS = List.of(
new Loan(1, "Ada", "Dune", LocalDate.of(2026, 11, 25), null),
new Loan(2, "Grace", "The Dispossessed", LocalDate.of(2026, 11, 21), null),
new Loan(3, "Alan", "Snow Crash", LocalDate.of(2026, 11, 14), null),
new Loan(4, "Edsger", "Programming Pearls", LocalDate.of(2026, 11, 10), LocalDate.of(2026, 11, 18)));
static final String Q = String.valueOf((char) 34);
static final String B = String.valueOf((char) 92);
static String field(String name, String value) {
return Q + name + Q + ":" + quote(value);
}
static String field(String name, int value) {
return Q + name + Q + ":" + value;
}
static String quote(String text) {
StringBuilder out = new StringBuilder(Q);
for (char c : text.toCharArray()) {
if (c == 34) {
out.append(B).append(Q);
} else if (c == 92) {
out.append(B).append(B);
} else {
out.append(c);
}
}
return out.append(Q).toString();
}
static String stateJson(State state) {
return switch (state) {
case OnLoan onLoan -> "{" + field("kind", "on_loan") + "," + field("days", (int) onLoan.daysLeft()) + "}";
case DueSoon dueSoon -> "{" + field("kind", "due_soon") + "," + field("days", (int) dueSoon.daysLeft()) + "}";
case Overdue overdue -> "{" + field("kind", "overdue") + "," + field("days", (int) overdue.daysLate()) + "}";
case Returned returned -> "{" + field("kind", "returned") + "," + field("on", returned.on().toString()) + "}";
};
}
static String array(List<Loan> loans, LocalDate today) {
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < loans.size(); i++) {
if (i > 0) {
out.append(',');
}
out.append(loans.get(i).toJson(today));
}
return out.append(']').toString();
}
record Reply(int status, String body) {
}
static List<Loan> overdue() {
List<Loan> result = new ArrayList<>();
for (Loan loan : LOANS) {
if (loan.state(TODAY) instanceof Overdue) {
result.add(loan);
}
}
return result;
}
static Reply route(String path) {
if ("/health".equals(path)) {
return new Reply(200, "{" + field("status", "ok") + "," + field("loans", LOANS.size()) + "}");
}
if ("/loans".equals(path)) {
return new Reply(200, array(LOANS, TODAY));
}
if ("/loans/overdue".equals(path)) {
return new Reply(200, array(overdue(), TODAY));
}
if (path.startsWith("/loans/")) {
try {
int id = Integer.parseInt(path.substring("/loans/".length()));
for (Loan loan : LOANS) {
if (loan.id() == id) {
return new Reply(200, loan.toJson(TODAY));
}
}
} catch (NumberFormatException e) {
return new Reply(400, "{" + field("error", "bad id") + "}");
}
return new Reply(404, "{" + field("error", "no such loan") + "}");
}
return new Reply(404, "{" + field("error", "no route") + "}");
}
static HttpServer start(int port) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", Desk::handle);
server.start();
return server;
}
static void handle(HttpExchange exchange) throws IOException {
Reply reply = route(exchange.getRequestURI().getPath());
send(exchange, reply.status(), reply.body());
}
static void send(HttpExchange exchange, int status, String body) throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/json; charset=utf-8");
exchange.sendResponseHeaders(status, bytes.length);
try (OutputStream out = exchange.getResponseBody()) {
out.write(bytes);
}
}
public static void main(String[] args) throws Exception {
if (args.length > 0 && "serve".equals(args[0])) {
HttpServer server = start(8091);
System.out.println("listening on " + server.getAddress().getPort());
new java.util.concurrent.CountDownLatch(1).await();
return;
}
HttpServer server = start(8091);
System.out.println("listening on " + server.getAddress().getPort());
try (HttpClient client = HttpClient.newHttpClient()) {
for (String path : List.of("/health", "/loans/2", "/loans/overdue", "/loans/9")) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:" + server.getAddress().getPort() + path))
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("GET " + path + " -> " + response.statusCode() + " " + response.body());
}
}
server.stop(0);
System.out.println("stopped");
}
}
The jar
The compile goes to classes and the jar takes that folder with a main class. The list shows the package folder and the eight class files the nested types produce; the run is the self test of the server page, from inside the archive:
javac -Xlint:all -d classes src/library/Desk.java
jar --create --file desk.jar --main-class library.Desk -C classes .
jar --list --file desk.jar
java -jar desk.jar
META-INF/
META-INF/MANIFEST.MF
library/
library/Desk$DueSoon.class
library/Desk$Loan.class
library/Desk$OnLoan.class
library/Desk$Overdue.class
library/Desk$Reply.class
library/Desk$Returned.class
library/Desk$State.class
library/Desk.class
listening on 8091
GET /health -> 200 {"status":"ok","loans":4}
GET /loans/2 -> 200 {"id":2,"member":"Grace","title":"The Dispossessed","due":"2026-11-21","state":{"kind":"due_soon","days":1}}
GET /loans/overdue -> 200 [{"id":3,"member":"Alan","title":"Snow Crash","due":"2026-11-14","state":{"kind":"overdue","days":6}}]
GET /loans/9 -> 404 {"error":"no such loan"}
stopped
The manifest
--main-class library.Desk writes the one manifest line the launcher needs, with the package name included. A jar built by hand without it answers no main manifest attribute, which is the same lesson as lesson 126. The nested records appear as Desk$Loan.class and so on, and they have to be in the archive: the sealed interface and its records are separate class files.
checkstyle
checkstyle reads the source against a configuration. The project has its own, four rules that name the mistakes a small service actually makes: braces around every block, no unused import, equals together with hashCode, and no empty block. The output of a clean run is two lines:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="NeedBraces"/>
<module name="UnusedImports"/>
<module name="EqualsHashCode"/>
<module name="EmptyBlock"/>
</module>
</module>
checkstyle -c checkstyle.xml src/library/Desk.java
Starting audit...
Audit done.
The rules of the project
The configuration is a file of the project and not a flag of the tool, so a build can name it and a reader can read it. It is shown here, before the command that uses it:
The Google configuration that ships in the jar is a different decision: it wants two-space indentation and a Javadoc comment on every public type and method, and on this file it would print a page of warnings about both. A project is free to choose the rules it enforces, and this one chose the four that catch a bug rather than a layout. The point of the tool is the choice, not the default.
PMD
PMD reads for patterns, and its quickstart set is the broad default. On this project it finds nothing, after two small changes the first run asked for: the string comparisons put the literal first, and the two HttpClient objects are closed with try-with-resources. Both are real improvements, which is the useful kind of finding. The command sends the two streams into one:
pmd check -d src/library -R rulesets/java/quickstart.xml -f text 2>&1
[WARN] Progressbar rendering conflicts with reporting to STDOUT. No progressbar will be shown. Try running with argument -r <file> to output the report to a file instead.
[WARN] This analysis could be faster, please consider using Incremental Analysis: https://docs.pmd-code.org/pmd-doc-7.29.0/pmd_userdocs_incremental_analysis.html
[INFO] Found no violations.
SpotBugs
SpotBugs reads the class files, so it runs after the compile and takes the folder, not the source. The command prints nothing and exits 0, which is its clean result:
spotbugs -textui classes
Silence is the report. A finding would be one line per bug with a rule code such as EI2, the way lesson 128 shows one on a class that stores its array. This project has no array to expose and no mutable static, so there is nothing to report.
The three tools together
The three look at different layers: checkstyle at the text, PMD at the source patterns, SpotBugs at the bytecode. A project runs all three because each is blind to what the others see. On this project the result is two lines of audit, three lines of PMD and silence, and that is the shape a small project should be able to show.
The next page
The next page is the pom that a Maven project would use, and the reason its build does not run in this image: the plugins are downloads and the container has no network. The jar of this page is what the JDK alone produces; Maven would wrap the same two commands in a lifecycle. The Java course covers the tools in lesson 128 and the jar in lesson 126.
The code on this page was compiled with OpenJDK 21.0.13, packed with jar and checked with checkstyle 14.4.0, PMD 7.29.0 and SpotBugs 4.9.8, on Alpine Linux in a Docker container. The output is the same on every run.
Article author: Arthur Isaev