public class NetInfo { public static void main(String[] args) { new NetInfo().say(); } public void say() { try { java.net.InetAddress i = java.net.InetAddress.getLocalHost(); System.out.println(i); // name and IP address System.out.println(i.getHostName()); // name System.out.println(i.getHostAddress()); // IP address only } catch(Exception e){e.printStackTrace();} } }
The output:
> java NetInfo realone/209.142.72.112 realone 209.142.72.112
To list all the interfaces available on a workstation:
import java.net.*; import java.util.*; import java.io.*; import java.nio.*; public class IPAdress { public void getInterfaces (){ try { Enumeration e = NetworkInterface.getNetworkInterfaces(); while(e.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e.nextElement(); System.out.println("Net interface: "+ni.getName()); Enumeration e2 = ni.getInetAddresses(); while (e2.hasMoreElements()){ InetAddress ip = (InetAddress) e2.nextElement(); System.out.println("IP address: "+ ip.toString()); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { IPAdress ip = new IPAdress(); ip.getInterfaces(); } }
The output:
> java IPAdress Net interface: lo IP address: /127.0.0.1 Net interface: eth0 IP address: /194.168.0.1 Net interface: eth1 IP address: /164.254.147.20 Net interface: ppp0 IP address: /64.68.115.69
Windows
A “low-tech” way to get the computer name (can be useful if there is no network card) is to use the environment variable COMPUTERNAME (at least on modern Windows installation).
[JDK1.4 or less] Pass it to your JVM as java -Dcomputername=”%COMPUTERNAME%” … and then get the value with System.getProperty(“computername”)
[JDK1.5 or more] You can extract environment variables directly with System.getenv(“COMPUTERNAME”)
Done! Happy coding!
Related posts:
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Consumer trong Java 8
Converting Java Date to OffsetDateTime
Java Program to Generate Random Hexadecimal Byte
JUnit 5 @Test Annotation
The Guide to RestTemplate
Get and Post Lists of Objects with RestTemplate
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement DelayQueue API
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Program to Implement LinkedHashSet API
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Hướng dẫn Java Design Pattern – Template Method
Filtering and Transforming Collections in Guava
Compare Two JSON Objects with Jackson
Java Program to Implement Queue using Linked List
Java Program to Solve the Fractional Knapsack Problem
Java Program to Represent Graph Using Incidence List
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Guide to Java 8’s Collectors
Object Type Casting in Java
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Quick sort
Lớp Collections trong Java (Collections Utility Class)
Java Program to Emulate N Dice Roller
Java Program to Implement Uniform-Cost Search
Java Program to Implement Bresenham Line Algorithm
Immutable Map Implementations in Java
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Shuffling Collections In Java