如果通过Java中的DNS无法解析,如何获取本地主机名?

Sha*_* D. 15 java hostname

这听起来像以前应该问过的东西,它有一些,但我希望获得机器的本地主机名和IP地址,即使它不能通过DNS解析(在Java中).

我可以通过迭代来获得没有解决方案的本地IP地址NetworkInterfaces.getNetworkInterfaces().

我发现这个问题的任何答案表明使用getLocalHost()

InetAddress localhost = java.net.InetAddress.getLocalHost();
hostName = localhost.getHostName();
Run Code Online (Sandbox Code Playgroud)

UnknownHostException如果主机名无法通过DNS解析,则抛出一个.

如果没有在幕后进行DNS查找,是否无法获取本地主机名?

edit:检索到的IP地址是10.4.168.23例外java.net.UnknownHostException: cms1.companyname.com: cms1.companyname.com(主机名已更改为伪匿名),hosts文件不包含主机名.但它确实知道它的主机名,所以我不知道为什么我不能在没有抛出异常的情况下得到它.

pet*_*erh 12

是的,在Java中应该有一种方法来获取主机名而不需要使用名称服务查找,但遗憾的是没有.

但是,你可以这样做:

if (System.getProperty("os.name").startsWith("Windows")) {
    // Windows will always set the 'COMPUTERNAME' variable
    return System.getenv("COMPUTERNAME");
} else {
    // If it is not Windows then it is most likely a Unix-like operating system
    // such as Solaris, AIX, HP-UX, Linux or MacOS.

    // Most modern shells (such as Bash or derivatives) sets the 
    // HOSTNAME variable so lets try that first.
    String hostname = System.getenv("HOSTNAME");
    if (hostname != null) {
       return hostname;
    } else {

       // If the above returns null *and* the OS is Unix-like
       // then you can try an exec() and read the output from the 
       // 'hostname' command which exist on all types of Unix/Linux.

       // If you are an OS other than Unix/Linux then you would have 
       // to do something else. For example on OpenVMS you would find 
       // it like this from the shell:  F$GETSYI("NODENAME") 
       // which you would probably also have to find from within Java 
       // via an exec() call.

       // If you are on zOS then who knows ??

       // etc, etc
    }
}
Run Code Online (Sandbox Code Playgroud)

这将使您在传统的Sun JDK平台(Windows,Solaris,Linux)上获得100%的所需,但如果您的操作系统更加激动(从Java角度来看),则会变得不那么容易.请参阅代码示例中的注释.

我希望有更好的方法.


Mal*_*alt 6

这个问题很老,但不幸的是仍然有用,因为用Java获取机器的主机名仍然不是一件容易的事.这是我在不同系统上进行一些测试运行的解决方案:

public static void main(String[] args) throws IOException {
        String OS = System.getProperty("os.name").toLowerCase();

        if (OS.indexOf("win") >= 0) {
            System.out.println("Windows computer name throguh env:\"" + System.getenv("COMPUTERNAME") + "\"");
            System.out.println("Windows computer name through exec:\"" + execReadToString("hostname") + "\"");
        } else {
            if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0) {
                System.out.println("Linux computer name throguh env:\"" + System.getenv("HOSTNAME") + "\"");
                System.out.println("Linux computer name through exec:\"" + execReadToString("hostname") + "\"");
                System.out.println("Linux computer name through /etc/hostname:\"" + execReadToString("cat /etc/hostname") + "\"");
            }
        }
    }

    public static String execReadToString(String execCommand) throws IOException {
        Process proc = Runtime.getRuntime().exec(execCommand);
        try (InputStream stream = proc.getInputStream()) {
            try (Scanner s = new Scanner(stream).useDelimiter("\\A")) {
                return s.hasNext() ? s.next() : "";
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

不同操作系统的结果:

OpenSuse 13.1

Linux computer name throguh env:"machinename"
Linux computer name through exec:"machinename
"
Linux computer name through /etc/hostname:""
Run Code Online (Sandbox Code Playgroud)

Ubuntu 14.04 LTS 这个有点奇怪,因为echo $HOSTNAME返回正确的主机名,但System.getenv("HOSTNAME")没有(这可能只是我的环境的问题):

Linux computer name throguh env:"null"
Linux computer name through exec:"machinename
"
Linux computer name through /etc/hostname:"machinename
"
Run Code Online (Sandbox Code Playgroud)

Windows 7的

Windows computer name throguh env:"MACHINENAME"
Windows computer name through exec:"machinename
"
Run Code Online (Sandbox Code Playgroud)

机器名称已被替换为(某些)匿名,但我保留了大写和结构.请注意执行时的额外换行符hostname,在某些情况下可能需要考虑它.


小智 1

如果您获得 127.0.0.1 作为 IP 地址,那么您可能需要找到操作系统特定的主机文件并在其中添加到主机名的映射。