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
Related Articles
Run shell script from java with Example