Java当前机器名称和登录用户? [英] Java current machine name and logged in user?
本文介绍了Java当前机器名称和登录用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以获取当前登录用户的名称(Windows / Unix)和机器的主机名?
Is it possible to get the name of the currently logged in user (Windows/Unix) and the hostname of the machine?
我认为它只是一个属性一些静态环境类。
I assume it's just a property of some static environment class.
我发现这个用户名是
com.sun.security.auth.module.NTSystem NTSystem = new
com.sun.security.auth.module.NTSystem();
System.out.println(NTSystem.getName());
这是机器名称:
import java.net.InetAddress;
...
String computerName;
...
try {
computerName = InetAddress.getLocalHost().getHostName();
}
catch(Exception ex) {
...
}
第一个仅适用于Windows吗?
Is the first one just for Windows?
如果您没有设置主机名,第二个会怎么做? ?
And what will the second one do, if you don't have a hostname set?
推荐答案
获取当前登录的用户:
System.getProperty("user.name"); //platform independent
和机器的主机名:
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());
这篇关于Java当前机器名称和登录用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文