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 Collections Interview Questions
A Guide to BitSet in Java
Simple Single Sign-On with Spring Security OAuth2
Check If Two Lists are Equal in Java
Spring’s RequestBody and ResponseBody Annotations
Java Program to Generate a Random Subset by Coin Flipping
Extra Login Fields with Spring Security
Intersection of Two Lists in Java
Database Migrations with Flyway
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Một số ký tự đặc biệt trong Java
Mapping Nested Values with Jackson
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Enum trong java
Merging Two Maps with Java 8
Introduction to Spring Method Security
Java Program to Implement Queue
Hướng dẫn Java Design Pattern – DAO
Xử lý ngoại lệ trong Java (Exception Handling)
The Basics of Java Security
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
A Guide to the ViewResolver in Spring MVC
Uploading MultipartFile with Spring RestTemplate
Java Program to Implement Expression Tree
Java Program to Check whether Undirected Graph is Connected using BFS
MyBatis with Spring
Spring @RequestParam Annotation
Java Program to add two large numbers using Linked List
Spring Cloud AWS – RDS
Java Program to Find Nearest Neighbor for Static Data Set
Using a Spring Cloud App Starter