Solved

Java InetAddress.getByName returns the wrong host name

Posted on 2016-09-21
2
23 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Learn about cloud computing and its benefits for small business owners.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

751 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