Java Utililty Methods Host Resolve

List of utility methods to do Host Resolve

Description

The list of methods to do Host Resolve are organized into topic(s).

Method

URIresolve(String host, String path)
resolve
URI uri = null;
try {
    uri = new URI(host);
    uri = uri.resolve(path);
} catch (URISyntaxException e) {
    e.printStackTrace();
    return null;
return uri;
StringresolveHost(String hostname)
Resolves hostname and returns ip address as a string.
try {
    InetAddress addr = InetAddress.getByName(hostname);
    byte[] ipAddr = addr.getAddress();
    StringBuilder ipAddrStr = new StringBuilder(15);
    for (int i = 0; i < ipAddr.length; i++) {
        if (i > 0)
            ipAddrStr.append('.');
        ipAddrStr.append(ipAddr[i] & 0xFF);
...
StringresolveHostname()
Helper utility that calls into the InetAddress system to resolve the hostname.
try {
    return InetAddress.getLocalHost().getCanonicalHostName();
} catch (java.net.UnknownHostException uhe) { 
    return "unresolvable";
StringresolveHostname()
resolve Hostname
InetAddress addr;
try {
    addr = InetAddress.getLocalHost();
} catch (UnknownHostException uhe) {
    return "localhost";
return addr.getCanonicalHostName();
StringresolveHostName()
Read hostName from JDK.
try {
    if (useInetAddress) {
        return InetAddress.getLocalHost().getHostName();
    throw new UnknownHostException("Do not use the Inet Adress");
} catch (UnknownHostException e) {
    throw new IllegalArgumentException("Cannot find the target host by itself", e);
StringresolveHostName(final String hostName)
Resolves the host name and returns its IP address.
InetAddress address = null;
address = InetAddress.getByName(hostName);
return address.getHostAddress();
StringresolveHostName(String hostname)
Resolves a given hostname by a canonical hostname.
if (hostname == null || hostname.isEmpty()) {
    return null;
return InetAddress.getByName(hostname).getCanonicalHostName();