UnknownHostException
JavaERRORNotableNetwork

DNS resolution failed for hostname

Quick Answer

Verify the hostname spelling and that DNS is reachable from the running environment (container, VPC).

What this means

Thrown when the IP address of a host cannot be determined because DNS resolution failed.

Why it happens
  1. 1Typo in hostname string
  2. 2DNS unreachable inside container or cloud VPC

Fix

Validate hostname and catch gracefully

Validate hostname and catch gracefully
try {
    InetAddress.getByName(host);
} catch (UnknownHostException e) {
    log.error("DNS failed for: {}", host);
}

Why this works

Catching early provides a clear message — DNS failures are often environment-specific.

Code examples
Triggerjava
InetAddress.getByName("not.a.real.host"); // UnknownHostException
Fallback to cached responsejava
try { return http.get(url); }
catch (UnknownHostException e) { return cachedResponse(url); }
Debug DNS in containerjava
// In shell: nslookup myservice.internal
// JVM override: -Dsun.net.spi.nameservice.provider.1=dns,sun
Sources
Official documentation ↗

Java SE Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Java errors