Title: Run a Lighthouse audit in Java — working code
Open Graph Title: Run a Lighthouse audit in Java — working code
X Title: Run a Lighthouse audit in Java — working code
Description: Run Lighthouse from Java and print the four scores, LCP, CLS and TBT: sync, async with a webhook, and retries. Tested code.
Open Graph Description: Run Lighthouse from Java and print the four scores, LCP, CLS and TBT: sync, async with a webhook, and retries. Tested code.
X Description: Run Lighthouse from Java and print the four scores, LCP, CLS and TBT: sync, async with a webhook, and retries. Tested code.
Mail addresses
contact@urlpipe.dev
Opengraph URL: https://urlpipe.dev/code/java/lighthouse-audit
Domain: urlpipe.dev
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://urlpipe.dev/#organization","name":"URLpipe","url":"https://urlpipe.dev","logo":"https://urlpipe.dev/icon.png","sameAs":["https://github.com/URLpipe"],"founder":{"@type":"Person","name":"Roger Campos","url":"https://rogercampos.com"}},{"@type":"WebSite","@id":"https://urlpipe.dev/#website","url":"https://urlpipe.dev","name":"URLpipe","publisher":{"@id":"https://urlpipe.dev/#organization"}}]}
{"@context":"https://schema.org","@graph":[{"@type":"TechArticle","@id":"https://urlpipe.dev/code/java/lighthouse-audit#article","headline":"Run a Lighthouse audit in Java — working code","description":"Run Lighthouse from Java and print the four scores, LCP, CLS and TBT: sync, async with a webhook, and retries. Tested code.","url":"https://urlpipe.dev/code/java/lighthouse-audit","mainEntityOfPage":"https://urlpipe.dev/code/java/lighthouse-audit","datePublished":"2026-09-24","dateModified":"2026-09-24","author":{"@type":"Person","name":"Roger Campos","url":"https://rogercampos.com"},"publisher":{"@id":"https://urlpipe.dev/#organization"},"image":"https://urlpipe.dev/og-image.png"},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Mobile or desktop — which device should I audit?","acceptedAnswer":{"@type":"Answer","text":"Mobile, unless you know your traffic is desktop. It emulates a 360×640 phone with a 4× slower CPU and a slow 4G network, which gives the lower, more telling scores. Pass device: \"desktop\" for a 1350×940 window with no throttling."}},{"@type":"Question","name":"Why isn't INP in the results?","acceptedAnswer":{"@type":"Answer","text":"INP is measured from real users interacting with the page, which a lab audit cannot do. Total Blocking Time is the lab metric that tracks it."}},{"@type":"Question","name":"Why use the async variant for Lighthouse?","acceptedAnswer":{"@type":"Answer","text":"An audit takes around 15 seconds, longer on a heavy page. Async hands you a token at once and delivers the report to your webhook, so nothing holds a connection open waiting."}},{"@type":"Question","name":"Do I need an SDK to call URLpipe from Java?","acceptedAnswer":{"@type":"Answer","text":"No SDK. HttpClient is in the JDK; the programs that read JSON add Gson, one jar, because the JDK has no parser."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://urlpipe.dev/"},{"@type":"ListItem","position":2,"name":"Code recipes","item":"https://urlpipe.dev/code"},{"@type":"ListItem","position":3,"name":"Java","item":"https://urlpipe.dev/code/java"},{"@type":"ListItem","position":4,"name":"Run a Lighthouse audit in Java","item":"https://urlpipe.dev/code/java/lighthouse-audit"}]},{"@type":"HowTo","name":"Run a Lighthouse audit in Java","description":"To run a Lighthouse audit in Java, POST a URL and a device to https://urlpipe.dev/lighthouse and read the JSON with Gson's JsonParser: four category scores from 0 to 1 and the lab metrics, LCP, CLS and TBT among them. An audit takes around 15 seconds, so the async variant with a webhook suits production. It costs 2 credits.","url":"https://urlpipe.dev/code/java/lighthouse-audit","tool":[{"@type":"HowToTool","name":"Java 17+"}],"step":[{"@type":"HowToStep","name":"Before you start","text":"Java 17 or later: java.net.http is in the JDK. Every program on this page reads JSON, and the JDK has no parser, so those use Gson — one jar, no dependencies of its own. In a Maven or Gradle build add com.google.code.gson:gson:2.13.1; to try a file on its own, download the jar next to it.","url":"https://urlpipe.dev/code/java/lighthouse-audit#setup"},{"@type":"HowToStep","name":"Print the four scores, LCP, CLS and TBT","text":""sync": true keeps the request open until the result is ready. BodyHandlers.ofString() reads the whole body as text, decoded with the charset the response names. HttpClient does not throw on a 4xx or 5xx, so check statusCode(). Gson's JsonParser reads the report; dig returns null for a category or metric Lighthouse could not compute.","url":"https://urlpipe.dev/code/java/lighthouse-audit#sync"},{"@type":"HowToStep","name":"The async variant: a token, a webhook and a poll","text":"Leave out sync and the answer is a token, straight away. One HttpClient is shared by every call — it holds the connection pool, so build it once — and a switch expression covers the answers GET /result/:token can give.","url":"https://urlpipe.dev/code/java/lighthouse-audit#async"},{"@type":"HowToStep","name":"Handle errors and retries","text":"A checked URLpipeException makes the caller decide what a refusal means. firstValueAsLong reads Retry-After without a parse of your own, and a body that is not JSON (a proxy in front of the API can answer in HTML) falls back to an empty object.","url":"https://urlpipe.dev/code/java/lighthouse-audit#errors"}]}]}
| view-transition | same-origin |
| csrf-param | authenticity_token |
| csrf-token | U3Q3XEGDUVdnEAKhYgYtQBU4hYyhWAxFRjEZW7xqPGV4aPR0EkOGv35wEhddSTUNc1iOICG79cVrIfbXyr2fFg |
| csp-nonce | 2c380dacc20f1961676866af8ed90831 |
| og:type | website |
| og:site_name | URLpipe |
| og:locale | en_US |
| og:image | https://urlpipe.dev/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
| og:image:alt | URLpipe — URL in. Clean data out. |
| twitter:card | summary_large_image |
| twitter:image | https://urlpipe.dev/og-image.png |
| twitter:image:alt | URLpipe — URL in. Clean data out. |
| theme-color | #3b82f6 |
Links:
Viewport: width=device-width,initial-scale=1