InetAddress.getLocalHost().getHostName() 抛出 UnknownHostException
InetAddress.getLocalHost().getHostName() throws UnknownHostException
我正在尝试从服务器上获取主机名 运行。
Java代码:
import java.net.InetAddress;
System.out.println("Host Name: " + InetAddress.getLocalHost().getHostName());
System.exit(0);
输出:
java.net.UnknownHostException: ThinkPad-Edge-E430: ThinkPad-Edge-E430: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
at MailQ.main(MailQ.java:45)
Caused by: java.net.UnknownHostException: ThinkPad-Edge-E430: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
... 1 more
主持人:
127.0.0.1 localhost #admin.local.com
#127.0.1.1 ThinkPad-Edge-E430
192.168.81.238 admin.local.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我找不到这里的问题。有人能帮帮我吗?
您可以检查:-
System.getProperty("os.name");
然后为该操作系统使用正确的环境变量,即
Windows
System.getenv("COMPUTERNAME");
Linux
System.getenv("HOSTNAME");
这种方法的问题在于,当您在不太常见的操作系统上启动 运行 时,您可能需要挖掘环境变量。
关于失败的原因,我相信您可能通过查看此 post 找到答案:-
Recommended way to get hostname in Java
Any attempt to determine the hostname by an IP address like this
InetAddress.getLocalHost().getHostName()
is bound to fail in some circumstances:
试试这个:
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author Crunchify.com
*/
public class CrunchifyGetIPHostname {
public static void main(String[] args) {
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
在线测试此代码
我从http://crunchify.com/how-to-get-server-ip-address-and-hostname-in-java/
那里拿走了它
我正在尝试从服务器上获取主机名 运行。
Java代码:
import java.net.InetAddress;
System.out.println("Host Name: " + InetAddress.getLocalHost().getHostName());
System.exit(0);
输出:
java.net.UnknownHostException: ThinkPad-Edge-E430: ThinkPad-Edge-E430: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
at MailQ.main(MailQ.java:45)
Caused by: java.net.UnknownHostException: ThinkPad-Edge-E430: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
... 1 more
主持人:
127.0.0.1 localhost #admin.local.com
#127.0.1.1 ThinkPad-Edge-E430
192.168.81.238 admin.local.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
我找不到这里的问题。有人能帮帮我吗?
您可以检查:-
System.getProperty("os.name");
然后为该操作系统使用正确的环境变量,即
Windows
System.getenv("COMPUTERNAME");
Linux
System.getenv("HOSTNAME");
这种方法的问题在于,当您在不太常见的操作系统上启动 运行 时,您可能需要挖掘环境变量。
关于失败的原因,我相信您可能通过查看此 post 找到答案:-
Recommended way to get hostname in Java
Any attempt to determine the hostname by an IP address like this
InetAddress.getLocalHost().getHostName()
is bound to fail in some circumstances:
试试这个:
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author Crunchify.com
*/
public class CrunchifyGetIPHostname {
public static void main(String[] args) {
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
在线测试此代码
我从http://crunchify.com/how-to-get-server-ip-address-and-hostname-in-java/
那里拿走了它