Unable to resolve hostname to IP
828710Jan 5 2011 — edited Jan 5 2011Hi,
I have a query which seems unusual but very important to get my job done. I'm trying to resolve the hostname to IP on a linux platform (Cent OS). Have a sample code how I'm trying to achieve the same.
TestJava Class:
----------------
import java.net.*;
import java.util.*;
public class TestJava {
public static void main(String [] args) {
String IP="10.10.1.7";
try {
InetAddress [] addr = InetAddress.getAllByName(args[0]);
for(InetAddress ad : addr)
if(ad.getHostAddress().equals(IP))
System.out.println("Success");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Output
--------
-bash-3.2# java TestJava _viscore
java.net.UnknownHostException: _viscore
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
at java.net.InetAddress.getAllByName(InetAddress.java:1084)
at java.net.InetAddress.getAllByName(InetAddress.java:1020)
at TestJava.main(TestJava.java:7)
NSLookup Result:
---------------------
-bash-3.2# nslookup _viscore
Server: 10.10.1.1
Address: 10.10.1.1#53
Name: _viscore.BDC.Maveric.local
Address: 10.10.1.7
Java Version:
------------------
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) Client VM (build 17.0-b16, mixed mode, sharing)
All necessary setup to establish connection to DNS is done in "/etc/resolv.conf"
Hostnames like "_hostname", "host/name" are valid and could able to do nslookup to these from console. But the same is failing from java end.
Any help is highly appreciated.
---
Manjunath