提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取主机名
//时 代 J a v a - nowjava.com 提 供 //package com.nowjava; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { public static void main(String[] argv) throws Exception { System.out.println(getHostName()); } public static String getHostName() { String server = null; // Obtain server name of local host try { final InetAddress localMachine = InetAddress.getLocalHost(); server = localMachine.getHostName();// 来 自 时 代 Java 公 众 号 - nowjava.com } catch (final UnknownHostException e) { server = "Unknown"; } return server; } public static String getHostName(String ipAddress) { String server = null; try { // Get hostname by textual representation of IP address final InetAddress addr =