How to get the IP address of the current machine using Java

2 Answers

0 votes
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MyClass {
    public static void main(String args[]) throws UnknownHostException {
        
        InetAddress ip = InetAddress.getLocalHost();
        
        System.out.println("IP address : " + ip.getHostAddress());
    }
}
 
 
 
 
/*
run:
   
IP address : 127.0.0.1
 
*/

 



answered Nov 3, 2023 by avibootz
0 votes
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MyClass {
    public static void main(String args[]) {
        
        try {
            InetAddress ip = InetAddress.getLocalHost();
            System.out.println("IP address : " + ip.getHostAddress());
        } catch (UnknownHostException e) {
            System.out.println("Exception: " + e);
        }
    }
}
 
 
 
 
/*
run:
   
IP address : 127.0.0.1
 
*/

 



answered Nov 3, 2023 by avibootz

Related questions

2 answers 162 views
2 answers 227 views
1 answer 104 views
1 answer 175 views
175 views asked Feb 21, 2019 by avibootz
2 answers 201 views
201 views asked Jun 23, 2016 by avibootz
...