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 Construct a Random Graph by the Method of Random Edge Selection
Allow user:password in URL
Converting a Stack Trace to a String in Java
Logging in Spring Boot
Extract links from an HTML page
Java Program to Implement the Bin Packing Algorithm
Getting Started with GraphQL and Spring Boot
Spring Boot with Multiple SQL Import Files
Biểu thức Lambda trong Java 8 – Lambda Expressions
Create a Custom Exception in Java
Java Program to Implement Red Black Tree
Constructor Dependency Injection in Spring
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Hướng dẫn Java Design Pattern – Object Pool
HttpClient Basic Authentication
Java Program to Implement Doubly Linked List
Spring Boot - CORS Support
Servlet 3 Async Support with Spring MVC and Spring Security
Shuffling Collections In Java
How to Set TLS Version in Apache HttpClient
Java Program to Find Nearest Neighbor for Dynamic Data Set
Spring Boot - Enabling Swagger2
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Inheritance with Jackson
Spring Data – CrudRepository save() Method
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Converting a List to String in Java
Java Program to Implement Ternary Heap
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Tổng quan về ngôn ngữ lập trình java
Cài đặt và sử dụng Swagger UI