solairis.com

Notes - Java - How to Get Your IP Address

Get your IP Address in Java.

import java.net.InetAddress;
import java.net.UnknownHostException;

public class getIP {

	public static void main(String[] args) {
		
		try {


			InetAddress addr = InetAddress.getLocalHost();
			

			String myIP = addr.getHostAddress();

			// Bonus. Get your hostname.
			String myHost = addr.getHostName();


			System.out.println(myIP + " " + myHost);

		} catch (UnknownHostException e) {
			System.out.println("Unknown Host: "+e);
		}

	}

}