Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java InetAddress.getByName returns the wrong host name

Posted on 2016-09-21
2
Medium Priority
?
33 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 2000 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

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.
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 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:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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