---
title: "Get machine name from IP"
author: "louisling"
date: "2009-09-01T16:07:29Z"
tags: ["Java"]
slug: "get-machine-name-from-ip"
draft: false
lastmod: "2009-09-01T16:07:29Z"
id: 460153
url: "https://louisling.iteye.com/blog/460153"
---

```java
public static void main(String[] args) throws Exception {
    String addr = getMachineNameFromIP("192.168.1.159");
    System.out.println(addr);
}

static String getMachineNameFromIP(String ipAddr) {
    try {
        InetAddress i = InetAddress.getByName(ipAddr);
        return i.getHostName();
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return "NULL";
    }
}
```