In this example, we will take a look at the Java code to get the IP address of the Client computer where this code is executed,
We can make use of the getLocalHost() method from InetAddress class that is present in java.net package.
Example: Code to get the IP Addressimport java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Java Program to identify local
* IP address
*
* Author: Code2care.org
* Date: 03-Apr-2022
* Version: 1.0
*
*/
public class JavaGetIPAddressExample {
public static void main(String[] args) throws UnknownHostException {
System.out.println(getLocalIPAddress());
}
public static String getLocalIPAddress() throws UnknownHostException {
InetAddress ipAddress =InetAddress.getLocalHost();
String ipAddressString = ipAddress.getHostAddress();
return ipAddressString;
}
}
Output:
192.168.0.122
More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Drop table using Java JDBC Template - Java
- Call PHP function on Button click using jquery ajax - PHP
- How to create SharePoint Document Library - SharePoint
- [Fix] Docker Error response from daemon: manifest for :latest not found: manifest unknown - Docker
- How to start Microsoft Teams on Mac Startup - Teams
- Twitter is down? Issues with Tweet create events affecting APIs - Twitter
- [Fix] MySQL No database selected - ERROR 1046 (3D000) - MySQL
- Change Current User Password using Mac Terminal Command - MacOS