InetAddress.getAllByName() only returns IPv4 addresses when it should return IPv6 ones as well

Solution Verified - Updated -

Issue

In Java, an InetAddress.getAllByName() call should return all IP addresses associated to a hostname. Under certain conditions even though both IPv4 and IPv6 addresses should be returned, only IPv4 addresses are resolved.

So given an /etc/hosts file like the following:

c44:b0c5:0:1101::ae9:f016   foo
10.10.240.22    foo

A java program like the following:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class test {
  public static void main(String[] args) {
    try {
      InetAddress SW[] = InetAddress.getAllByName("foo"); 
      for (int i=0; i<SW.length; i++) 
        System.out.println("InetAddress.getAllByName(): " + SW[i]); 
    }
    catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}

When launched normally with bash will return:

InetAddress.getAllByName(): foo/10.233.240.22
InetAddress.getAllByName(): foo/c44:b0c5:0:1101:0:0:ae9:f016

When launched with ksh with the pipe "|" directive it will return only IPv4 addresses:

$ ksh -c "echo y | java test"
InetAddress.getAllByName(): foo/10.233.240.22

Environment

  • Red Hat Enterprise Linux
  • Java (both OpenJDK and Oracle)

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.