문제
현재 로그인 한 사용자 (Windows/Unix)와 컴퓨터의 호스트 이름을 얻을 수 있습니까?
나는 그것이 정적 환경 클래스의 속성 일 뿐이라고 생각합니다.
사용자 이름에 대해 이것을 찾았습니다
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) {
...
}
창문을위한 첫 번째 것이 있습니까?
그리고 호스트 이름 세트가 없다면 두 번째는 무엇을 할 것인가?
해결책
현재 로그인 한 사용자를 얻으려면 :
System.getProperty("user.name"); //platform independent
그리고 기계의 호스트 이름 :
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());
다른 팁
현재 로그인 한 사용자를 얻으려면 :
System.getProperty("user.name");
기계의 호스트 이름을 얻으려면 :
InetAddress.getLocalHost().getHostName();
질문의 마지막 부분에 답하기 위해 Java API gethostname ()가 돌아올 것이라고 말합니다
이 IP 주소의 호스트 이름 또는 보안 검사에 의해 작업이 허용되지 않는 경우 IP 주소의 텍스트 표현.
사용 user.name
환경 변수를 가짜로 만들 수 있으므로 안전하지 않습니다. 사용중인 방법은 양호하고 UNIX 기반 OS에 대한 유사한 방법이 있습니다.
현재 로그인 한 사용자 경로를 얻으려면 :
System.getProperty("user.home");
제휴하지 않습니다 StackOverflow