Categories
java

how to get the hostname in java code

In this post, we will learn how to get the hostname in java code

In this example we are using InetAddress to get the hostname

Syntax
InetAddress.getLocalHost().getHostName();
Example
import java.net.InetAddress;

public class Hostname {

	public static void main(String[] args) {
		try {

			String hostName = InetAddress.getLocalHost().getHostName();
			System.out.println(hostName);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Output
DESKTOP-U9R6O19
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/Hostname.java

Related Articles

Run shell script from java with Example

 

Leave a Reply

Your email address will not be published. Required fields are marked *