0

When I try to get the hostname of my machine from the code, I'm using the following:

address = InetAddress.getLocalHost().getCanonicalHostName();

The above throws an excpetion (UnknownHostException) if the host is composed of dots (for example "My.Host") or _.

I still am interested in the above host names. Is there any other option to fetch them?

Maoritzio
  • 1,142
  • 2
  • 13
  • 31

1 Answers1

0

You may better try to use this:

InetAddress.getLocalHost().getHostName()

getCanonicalHostName() will do a reverse DNS lookup, whereas getHostName() would return the stored hostname.

Also check getHostName()

Returns the IP address string in textual presentation.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331