2

I am trying to get the host name off the server where it is running.

Java code:

import java.net.InetAddress; 
System.out.println("Host Name: " + InetAddress.getLocalHost().getHostName());
System.exit(0);

Output:

java.net.UnknownHostException: ThinkPad-Edge-E430: ThinkPad-Edge-E430: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
at MailQ.main(MailQ.java:45)
Caused by: java.net.UnknownHostException: ThinkPad-Edge-E430: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
... 1 more

Hosts:

127.0.0.1   localhost #admin.local.com
#127.0.1.1  ThinkPad-Edge-E430
192.168.81.238 admin.local.com
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

I cant find what is the issue here. Can some help me out?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Bishwarup Das
  • 681
  • 1
  • 12
  • 21
  • Possible duplicate of http://stackoverflow.com/questions/4969156/error-java-net-unknownhostexception – Raedwald Jul 14 '15 at 15:12

3 Answers3

1

You could just check: -

System.getProperty("os.name");

Then use the correct environment variable for that operating system, i.e.

Windows

System.getenv("COMPUTERNAME");

Linux

System.getenv("HOSTNAME");

The problem with this approach is that when you start running on operating systems that are less common, you may have to dig for the environment variables.

For the reasons as to why it's failing, I believe your answer is probably found by looking at this post: -

Recommended way to get hostname in Java

Any attempt to determine the hostname by an IP address like this

InetAddress.getLocalHost().getHostName()

is bound to fail in some circumstances:

Community
  • 1
  • 1
ed_me
  • 3,338
  • 2
  • 24
  • 34
0

This is a bit old but no one seemed to have spotted that you have commented out the entry for ThinkPad-Edge-E430 in your hosts file. It also had a typo in the ip address.

Change:

#127.0.1.1  ThinkPad-Edge-E430

To:

127.0.0.1  ThinkPad-Edge-E430

and you should be ok.

David Newcomb
  • 10,639
  • 3
  • 49
  • 62
-1

try this:

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Crunchify.com
 */

public class CrunchifyGetIPHostname {

    public static void main(String[] args) {

        InetAddress ip;
        String hostname;
        try {
            ip = InetAddress.getLocalHost();
            hostname = ip.getHostName();
            System.out.println("Your current IP address : " + ip);
            System.out.println("Your current Hostname : " + hostname);

        } catch (UnknownHostException e) {

            e.printStackTrace();
        }
    }
}

you test this code online on http://www.browxy.com/

i have taken it from http://crunchify.com/how-to-get-server-ip-address-and-hostname-in-java/

Arthur Tsidkilov
  • 5,401
  • 2
  • 21
  • 18
  • I may be missing something, but this code seems to do the same as the code of the OP. The only difference is that this code catches the exception. But the problem will still be there. – Montecarlo Apr 04 '17 at 17:19