Q:

Java program to get system IP Address in Windows or Linux Machine

belongs to collection: Java Most Popular & Searched Programs

0

Java program to get system IP Address in Windows or Linux Machine

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Get IP Address of Linux/Windows based Machine using Java program: This program will get and print the IP Address of Linux or Windows operating system based machines.

package com.includehelp;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * Program to get system IP Address in Windows or Linux Machine
 * @author includehelp
 */
public class SystemIPAddress {
    /**
     * Method for Get System IP Address
     * @return IP Address
     */
    public static String getSystemIP(){
        try{
            String sysIP="";
            String OSName=  System.getProperty("os.name");
	    if(OSName.contains("Windows")){
                sysIP   =InetAddress.getLocalHost().getHostAddress();
	    }
	    else{
	    	sysIP=getSystemIP4Linux("eth0");
	    	if(sysIP==null){
                    sysIP=getSystemIP4Linux("eth1");
		    if(sysIP==null){
		  	sysIP=getSystemIP4Linux("eth2");
                        if(sysIP==null){
                            sysIP=getSystemIP4Linux("usb0");
                        }
                    }
	   	}
	    }
	    return sysIP;
	}
	catch(Exception E){
            System.err.println("System IP Exp : "+E.getMessage());
            return null;
	}
    }
    
    /**
     * method for get IP of linux System
     * @param name
     * @return 
     */
    private static String getSystemIP4Linux(String name){
        try{
            String ip="";
            NetworkInterface networkInterface = NetworkInterface.getByName(name);
            Enumeration<InetAddress> inetAddress = networkInterface.getInetAddresses();
            InetAddress currentAddress = inetAddress.nextElement();
            while(inetAddress.hasMoreElements()){
                currentAddress = inetAddress.nextElement();
                if(currentAddress instanceof Inet4Address && !currentAddress.isLoopbackAddress()){
                    ip = currentAddress.toString();
                    break;
                }
            }
            if(ip.startsWith("/")){
                ip=ip.substring(1);
            }
            return ip;
        } 
        catch (Exception E) {
            System.err.println("System Linux IP Exp : "+E.getMessage());
            return null;
        }
    }
    
    public static void main(String[] args) {
        String ipAddress = getSystemIP();
        System.out.println("System IP : "+ipAddress);
    }
}

Output

System IP : 145.168.10.77

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to get System MAC Address of Windows ... >>
<< Java program to print Screen Resolution...