Java compile check after edit
Java compile errors caught right after an edit
Runs the project's build-tool compile task (mvn -q compile or ./gradlew -q compileJava) on the modified .java file and reports compile errors so they are fixed within the same loop.
What does the Java compile check after edit hook do?
Java compile check after edit is a Claude Code PostToolUse hook matching Write|Edit. It fires automatically at that lifecycle event — outside the model, so it can't be skipped or forgotten. Java compile errors caught right after an edit.
As a PostToolUse hook it runs after the action, reacting to what just happened rather than blocking it. Because it is a deterministic Node.js script, it executes on every matching event without relying on the model to remember — the guarantee that makes agentic workflows safe to automate.
Use cases
- Catch compile errors immediately after an edit
- Surface broken imports before they accumulate
- Pair with the formatter for a complete Java quality pipeline
Tags
settings.json fragment
{
"hooks": {
"PostToolUse": [
{
"hooks": [
{
"command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/java-compile.mjs",
"type": "command"
}
],
"matcher": "Write|Edit"
}
]
}
}Script · .claude/hooks/java-compile.mjs
#!/usr/bin/env node
// @hookstack post-edit-java-compile
// Vérifie la compilation Java après édition (PostToolUse Write|Edit)
import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { detectBuildTool } from "./lib/java-build.mjs";
function makeDefaultExec(projectDir) {
return (cmd) =>
execSync(cmd, { cwd: projectDir, stdio: "pipe", timeout: 60_000 });
}
export function run(
input,
{ exec, exists, projectDir = process.env.CLAUDE_PROJECT_DIR, platform } = {},
) {
const filePath = input.tool_input?.file_path ?? input.tool_input?.path ?? "";
if (!filePath.endsWith(".java")) return null;
const cwd = projectDir ?? process.cwd();
const build = detectBuildTool({ exists, projectDir: cwd, platform });
if (!build) return null; // pas d'outil de build Java reconnu
const doExec = exec ?? makeDefaultExec(cwd);
const task = build.tool === "maven" ? "compile" : "compileJava";
try {
doExec(`${build.cmd} -q ${task}`);
return null;
} catch (err) {
const output = err.stdout?.toString() ?? "";
return output ? { message: `[java-compile] ${output.trim()}\n` } : null;
}
}
/* v8 ignore next 5 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const input = JSON.parse(readFileSync(0, "utf8"));
const result = run(input);
if (result?.message) process.stderr.write(result.message);
}