// Import required package import java.net.InetAddress; import java.net.UnknownHostException; // Get the hostname try { InetAddress ip = InetAddress.getLocalHost(); String hostname = ip.getHostName(); } catch (UnknownHostException e) { e.printStackTrace(); }