Java Program to Print All Local IP Addresses
We can use the following Java command line program to print all loacal IP addresses of the computer. We iterate over all Net-Interfaces and get Inet Addresses.
package com.helloacm;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
public class Main {
public static void main(String[] args) {
try {
var allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements()) {
var netInterface = (NetworkInterface) allNetInterfaces.nextElement();
var addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
System.out.println(ip.getHostAddress());
}
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Example output from the command line:
127.0.0.1
192.168.1.94
192.168.56.1
172.28.224.1
See also: The DNS Lookup Tool in Java (InetAddress)
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses