Solved

Java InetAddress.getByName returns the wrong host name

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

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 28

Expert Comment

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

627 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