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:
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Hướng dẫn Java Design Pattern – State
The Java 8 Stream API Tutorial
Send email with JavaMail
Giới thiệu SOAP UI và thực hiện test Web Service
Multipart Upload with HttpClient 4
Introduction to Spring MVC HandlerInterceptor
How to Read HTTP Headers in Spring REST Controllers
Tổng quan về ngôn ngữ lập trình java
Java Program to Implement CopyOnWriteArrayList API
Guide to Character Encoding
Java Program to Implement Hash Tables with Linear Probing
Simple Single Sign-On with Spring Security OAuth2
Converting Java Date to OffsetDateTime
Chuyển đổi từ HashMap sang ArrayList
Request Method Not Supported (405) in Spring
Java Program to Implement ArrayBlockingQueue API
New Features in Java 9
Guide to the Java TransferQueue
JWT – Token-based Authentication trong Jersey 2.x
Exploring the Spring 5 WebFlux URL Matching
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Check Multiplicability of Two Matrices
Java Program to implement Sparse Vector
Apache Commons Collections SetUtils
Java Program to Implement Brent Cycle Algorithm
Create a Custom Auto-Configuration with Spring Boot
The Registration Process With Spring Security
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Randomized Binary Search Tree