JDK-6698625 : InetAddress.getLocalHost() failed in returning chinese local host name
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2003
  • CPU: x86
  • Submitted: 2008-05-07
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u10Fixed 7 b30Fixed
Description
FULL PRODUCT VERSION :
c:>java -version
java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
c:> ver
Microsoft Windows [������ 5.2.3790]

A DESCRIPTION OF THE PROBLEM :
the InetAddress.getLocalHost()  works fine in jre 1.5.0_03, but fails to fetch chinese local host name in jre 1.6.0_03.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
assume the host name is "��������_��������", which have to be a chinese presenting, and the running os is Windows 2003 Standard Edition with SP2


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
with jre 1.5.0_03, it print like:
host    : ��������_��������
-42 -48 -50 -60 95 -78 -30 -54 -44
it corretly presents my host name
ACTUAL -
with jre 1.6.0_03 , it prints like:
host    : ????_???
63 63 63 63 95 63 63 63


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
     try {
            InetAddress localHost = InetAddress.getLocalHost();
            String hostName = localHost.getHostName();
            
            System.out.println("host\t: " + hostName);
            byte[] bytes = hostName.getBytes();
            for(byte b : bytes){
                System.out.print(b + " ");
            }
            System.out.println();

        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I am going to use this localhost name to get all local IP addr later, but with "????", it is sure to throw java.net.UnknownHostException.

downgrade jre environment to 1.5.

Release Regression From : 5.0u3
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION The native implementation of lookupAllHostAddr incorrectly uses NewStringUTF to convert the platform dependant native string to a jstring. It of course should be invoking the String constructor that will use the platforms character encoding. Actually, this is not even necessary as we already have a reference to the jstring being passed as a parameter to this method. Simply use this reference.
09-06-2008