Java当前机器名称和登录用户?

时间:2022-06-03 11:59:12

Is it possible to get the name of the currently logged in user (Windows/Unix) and the hostname of the machine?

是否可能获取当前登录用户(Windows/Unix)的名称和计算机的主机名?

I assume it's just a property of some static environment class.

我假设它只是某个静态环境类的属性。

I've found this for the user name

我找到了这个用户名

com.sun.security.auth.module.NTSystem NTSystem = new
        com.sun.security.auth.module.NTSystem();
System.out.println(NTSystem.getName());

and this for the machine name:

这是为机器命名的:

import java.net.InetAddress;
...
String computerName;
...
try {
    computerName = InetAddress.getLocalHost().getHostName();
}

catch(Exception ex) {
    ...
}

Is the first one just for Windows?

第一个是Windows操作系统吗?

And what will the second one do, if you don't have a hostname set?

如果没有主机名集,第二个会怎么做?

4 个解决方案

#1


218  

To get the currently logged in user:

获取当前登录用户:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

机器的主机名:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());

#2


78  

To get the currently logged in user:

获取当前登录用户:

System.getProperty("user.name");

To get the host name of the machine:

获取机器的主机名:

InetAddress.getLocalHost().getHostName();

To answer the last part of your question, the Java API says that getHostName() will return

要回答问题的最后一部分,Java API说getHostName()将返回

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.

此IP地址的主机名,或者如果安全检查不允许操作,则是IP地址的文本表示。

#3


9  

Using user.name is not secure as environment variables can be faked. Method you were using is good, there are similar methods for unix based OS as well

使用user.name不安全,因为可以伪造环境变量。您使用的方法很好,基于unix的操作系统也有类似的方法。

#4


0  

To get the currently logged in user path:

获取当前登录用户路径:

System.getProperty("user.home");

#1


218  

To get the currently logged in user:

获取当前登录用户:

System.getProperty("user.name"); //platform independent 

and the hostname of the machine:

机器的主机名:

java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());

#2


78  

To get the currently logged in user:

获取当前登录用户:

System.getProperty("user.name");

To get the host name of the machine:

获取机器的主机名:

InetAddress.getLocalHost().getHostName();

To answer the last part of your question, the Java API says that getHostName() will return

要回答问题的最后一部分,Java API说getHostName()将返回

the host name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.

此IP地址的主机名,或者如果安全检查不允许操作,则是IP地址的文本表示。

#3


9  

Using user.name is not secure as environment variables can be faked. Method you were using is good, there are similar methods for unix based OS as well

使用user.name不安全,因为可以伪造环境变量。您使用的方法很好,基于unix的操作系统也有类似的方法。

#4


0  

To get the currently logged in user path:

获取当前登录用户路径:

System.getProperty("user.home");