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:
Hướng dẫn Java Design Pattern – Strategy
Java String Conversions
Spring Webflux with Kotlin
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Implement Booth Algorithm
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Beans and Dependency Injection
Java Program to Implement Sparse Matrix
Request Method Not Supported (405) in Spring
Java Program to Implement String Matching Using Vectors
Control the Session with Spring Security
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Find the Vertex Connectivity of a Graph
Hamcrest Collections Cookbook
Converting a Stack Trace to a String in Java
Find the Registered Spring Security Filters
JUnit 5 @Test Annotation
Spring Security Basic Authentication
Apache Camel with Spring Boot
Spring Boot - Hystrix
Spring Security Remember Me
Returning Custom Status Codes from Spring Controllers
Spring Boot - Logging
Spring Data – CrudRepository save() Method
New Features in Java 8
Sắp xếp trong Java 8
Getting Started with Stream Processing with Spring Cloud Data Flow
Check if there is mail waiting
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line