Solved

Java InetAddress.getByName returns the wrong host name

Posted on 2016-09-21
2
20 Views
Last Modified: 2016-10-10
Can someone explain to me what I need to check to resolve an apparent DNS issue?  I am using Java's InetAddress class to determine the host name of the machine the program is running on.  It is returning an incorrect machine name.  My guess is that a former employee configured something on this machine which is causing this issue.  I'm just not sure what he could have done.

The server is a Windows 2003 server.  The machine name is set correctly as far as I can tell.  I verified this two ways:

  1. Open Windows Explorer, right click on "My Computer", navigate to Properties > Computer Name.  This screen shows the correct computer name like this: "computer1.examplecompany.com.  Under "Full Computer Name" it shows Domain: "examplecompany.com".
  2. Open a command prompt and type the "hostname" command.  This returns the same machine name in the prior step without the domain.  In this example, it would be just "computer1" without "examplecompany.com".

Now when I attempt to use a Java program to get the host name, here is the relevant code fragment I am using:

InetAddress localhost = InetAddress.getLocalHost();
localhost = InetAddress.getByName(localhost.getHostAddress());
System.out.println("Hostname: " + localhost.getHostName());

Open in new window


The hostname that gets output to the console is completely different from the computer name.  In fact, if I perform an nslookup on the name that the Java program output, I get this response:  "Non-existent domain".

I don't know if this is relevant or not, but the name that the java program is returning is also in the "hosts" file with the same IP as the local machine (the IP is the company wide IP address, not 127.0.0.1).  I thought maybe that was the problem, so I changed the hosts file to use a different name, and this appeared to have no effect even after flushing the DNS cache.  

So what else can I check to see why this Java program is returning the wrong name?
0
Comment
Question by:jbaird123
  • 2
2 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points (awarded by participants)
ID: 41809921
I think what you are attempting is a "reverse DNS" lookup - which is a pretty complex process to debug.  At least I know our network operations team often spend a good deal of time solving issues around reverse DNS - since that basically involves the normal DNS chain (which can be an internal server or your ISP etc.) but working backwards from IP to name.

However, in this case it seems like you should have a simpler solution by just asking for the hostname.

In Windows "%COMPUTERNAME%" should be a defined environment variable which you can retrieve in Java through System.getenv():

https://docs.oracle.com/javase/tutorial/essential/environment/env.html

See if that works for you and avoid the whole "reverse DNS" issue.

Doug
0
 
LVL 27

Expert Comment

by:dpearson
ID: 41836581
No response from OP.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Learn about cloud computing and its benefits for small business owners.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question