Java 현재 컴퓨터 이름과 로그인 한 사용자? com.sun.security.auth.module.NTSystem(); System.out.println(NTSystem.getName()); 그리고

현재 로그인 한 사용자 이름 (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) {
    ...
}

첫 번째는 Windows 전용입니까?

호스트 이름이 설정되지 않은 경우 두 번째는 무엇을합니까?



답변

현재 로그인 한 사용자를 얻으려면 :

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환경 변수가 위조 될 수 있으므로 사용 이 안전하지 않습니다. 사용중인 방법이 좋으며 유닉스 기반 OS에도 비슷한 방법이 있습니다.


답변

현재 로그인 한 사용자 경로를 얻으려면

System.getProperty("user.home");