Solved

Java InetAddress.getByName returns the wrong host name

Posted on 2016-09-21
2
15 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 26

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 26

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

BIND is the most widely used Name Server. A Name Server is the one that translates a site name to it's IP address. There is a new bug in BIND (https://kb.isc.org/article/AA-01272), affecting all versions of BIND 9 from BIND 9.1.0 (inclusive) thro…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now