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:
The XOR Operator in Java
Spring Boot - CORS Support
Guide to java.util.concurrent.Future
OAuth2 Remember Me with Refresh Token
Java Program to Implement Tarjan Algorithm
Refactoring Design Pattern với tính năng mới trong Java 8
Spring MVC and the @ModelAttribute Annotation
Removing all duplicates from a List in Java
Java Program to Implement Hash Tree
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Test a REST API with Java
JUnit5 @RunWith
Java Program to Permute All Letters of an Input String
Retrieve User Information in Spring Security
Java Program to Find a Good Feedback Vertex Set
Spring Boot - Quick Start
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement IdentityHashMap API
Lập trình hướng đối tượng (OOPs) trong java
Guide to Java 8 groupingBy Collector
Spring Boot - Rest Template
Spring MVC Tutorial
Java Program to Show the Duality Transformation of Line and Point
Java Program to Implement Fermat Primality Test Algorithm
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
How to Count Duplicate Elements in Arraylist
Lập trình đa luồng với Callable và Future trong Java
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement AttributeList API
Java Program to Represent Linear Equations in Matrix Form
Guide to Dynamic Tests in Junit 5