Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Feb 292012
 

How to get the hostname of a local machine using Java

Answer:

The following codes illtsraeted how to get the hostname of the local machine using Java.

import java.net.InetAddress;

public class Test {

    public static void main(String[] args) {

        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get hostname
            String hostName = addr.getHostName();

            System.out.println(hostName);

        } catch (Exception e) {}
    }
}

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>