티스토리 뷰
Java 어플리케이션이 현재 동작하고있는 시스템의 운영체제 정보를 출력하기위해 작성한다.
대부분 개발을 진행할 때,
개발 / 운영 / Test 등으로 구분해서 다르게 설정한다.
원하는 호스트의 정보를 얻기위해
InetAddress 클래스를 사용한다. ** InetAddress 는 생성자가 존재하지 않는다**
InetAddress의 메서드
getByName() | Parameter에 해당되는 InetAddress 클래스 리턴 |
getHostName() | InetAddress의 호스트 주소 리턴 (String) |
getLocalHost() | 현재 실행중인 컴퓨터의 InetAddress 객체 리턴 |
getAddress() | 주소를 나타내는 4바이트 배열 리턴 |
getHostAddress() | IP주소를 마침표로 구분된 4자리 형식의 문자열로 반환 (String) |
getAllbyName() | Parameter에 해당되는 모든 InetAddress 클래스 리턴 |
IF(로컬)
public class Main {
public static void main(String[] args) {
//host array로 나올수있기때문에 for문으로 돌리고 0번째에 있는걸 사용.
System.out.println("호스트 정보 : " + getHostInfo());
}
public static String getHostInfo() {
String hostName = "";
String hostAddr = "";
try {
hostName = InetAddress.getLocalHost().getHostName();
hostAddr = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
// 예외처리
}
return hostName + "(" + hostAddr + ")";
}
}
호스트 정보 : DESKTOP-xxxxxxx(xxx.xxx.xxx.xxx)
Run as Java application으로 실행하면 컴퓨터의 정보(IP주소)가 출력된다.
만약 서버에 프로젝트를 올려서 실행중이라면,
호스트정보 : server.example.com (192.168.1.100) 이라고 출력될것이다.
'Spring > Tip' 카테고리의 다른 글
@Scheduled 스프링 스케쥴 (0) | 2024.07.30 |
---|---|
방어적 프로그래밍 (0) | 2024.07.04 |
[ object object ] (0) | 2024.07.03 |
Java - 서버 호스트 (Host) 정보 (2) (0) | 2024.06.21 |