How to get local computer name and IP in Java

  • 05 March 2018
  • ADM

 

How to get local computer name and IP in Java - images/logos/java.jpg

 

Here is a snippet code how to get the local computer name and IP in Java.

Code

package com.adm;

import java.net.InetAddress;

public class NetworkInfo {
    public static void main(String[] args) throws Exception {
	System.out.println("Get local computer name and IP:");
	System.out.println();

	InetAddress ip = InetAddress.getLocalHost();

	System.out.println("Name + IP: " + ip.toString());
	System.out.println("Name:" + ip.getHostName());
	System.out.println("IP address: " + ip.getHostAddress());
	System.out.println("Full name: " + ip.getCanonicalHostName());
    }
}

Output

Get local computer name and IP

Name + IP: ADMPC/192.168.56.1
Name:ADMPC
IP address: 192.168.56.1
Full name: ADMPC.admfactory.com

 

References