UnknownHostException

java.net.UnknownHostException: invalid.hostname.example

Quick Answer

DNS cannot resolve the hostname. Check for typos in the hostname and verify network/DNS configuration.

Why This Happens

UnknownHostException means the hostname could not be resolved to an IP address via DNS. This happens with misspelled hostnames, network connectivity issues, DNS server problems, or when using an internal hostname from a network that cannot resolve it.

The Problem

URL url = new URL("https://my-servr.example.com/api"); // Typo in hostname
url.openStream();

The Fix

URL url = new URL("https://my-server.example.com/api"); // Correct hostname
try (InputStream in = url.openStream()) {
    // Process response
} catch (UnknownHostException e) {
    System.err.println("Cannot resolve host: " + e.getMessage());
}

Step-by-Step Fix

  1. 1

    Identify the hostname

    Read the exception message to get the hostname that failed to resolve. Check for typos.

  2. 2

    Test DNS resolution

    Try resolving the hostname with nslookup or ping from the same machine to verify DNS works.

  3. 3

    Fix hostname or DNS config

    Correct the hostname, check /etc/hosts, verify DNS server configuration, or check network connectivity.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free