import java.net.InetAddress;
public class ComputerName {
/**
* @param args
*/
public static void main(String[] args) {
String computerName = null;
try {
computerName = InetAddress.getLocalHost().getHostName();
System.out.println(computerName);
} catch (Exception ex) {
}
}
InetAddress.getLocalHost().getHostName() 사용 시 UnknownHostException
// 아래처럼 사용할 경우 DNS구성이 되어있지 않으면 UnknownHostException이 발생한다.
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
// 웹 검색에서 찾은 방법 하나
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostname = e.getMessage(); // host = "hostname: hostname"
if ( hostname != null ) {
int colon = hostname.indexOf(':');
if ( colon>0 ) {
return hostname.substring(0, colon); // Dangerous, this is JVM dependant code.
}
}
e.printStackTrace();
}
'[공부용]참고 사이트 모음 > [java]' 카테고리의 다른 글
[java] 자바에서 날짜 변환하기 yyyy-mm-dd HH:mm:ss.SSS (0) | 2021.02.25 |
---|---|
[Java] String 의 특정 index의 문자열 교체 (0) | 2021.02.24 |
[java] 자바 2의 보수 16비트 바이너리 => 정수로 나타내는 법 (2) | 2020.12.10 |
자바 스레드 주기 만들기,Timer, ScheduledExecutorService (0) | 2020.11.30 |
[java] 소켓프로그래밍 udp DatagramSocket (0) | 2020.11.24 |