Get the workstation name or IP

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:

Custom Cascading in Spring Data MongoDB
Converting Between an Array and a Set in Java
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java 9 Stream API Improvements
Spring Boot - Google Cloud Platform
Java Program to Solve a Matching Problem for a Given Specific Case
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Implement Gabow Algorithm
Java Program to Implement Queue
Java Program to Implement Control Table
Adding Shutdown Hooks for JVM Applications
Anonymous Classes in Java
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
ExecutorService – Waiting for Threads to Finish
Hướng dẫn Java Design Pattern – Chain of Responsibility
Hướng dẫn sử dụng lớp Console trong java
Converting between an Array and a List in Java
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Hướng dẫn Java Design Pattern – Service Locator
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Abstract class và Interface trong Java
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Copy a List to Another List in Java
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Rolling Hash
Spring MVC Custom Validation
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Implement Sorted List
Java Program to Implement Stein GCD Algorithm
Java Program to Implement LinkedList API