Find Public IP Address in Java Programming
Here, we have posted a Simple Program that describes How To Find Public IP Address in Java Programming that you’re currently logged in. This Java Program is an IP Address Tracker Program wherein you can get the Public IP Address of your Machine only when you connect your System to the Internet.
What is an IP Address?
IP stands for Internet Protocol Address. It is a 32-Bit Address String. It is a Numerical Tag associated with every Electronic Device such as Mobile Phone, Laptop, Desktop, Printer, Router, etc that helps to identify every single device connected to a Network. It is primarily used for Communication with different devices connected on the same Network.
How Does This Program Works?
This Program makes use of InetAddress Class to get the Public IP Address of your machine via WhatIsMyAdddress.com. However, this Program works only when your System is connected to the Internet. It gives an Error when Internet is not connected which we have handled through Exception.
Find Public IP Address in Java Programming Language
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.net.*; import java.io.*; import java.util.*; class SystemIPAddress { public static void main(String[] arguments) { String systemipaddress = ""; try { URL url_name = new URL("http://bot.whatismyipaddress.com"); BufferedReader sc = new BufferedReader(new InputStreamReader(url_name.openStream())); systemipaddress = sc.readLine().trim(); if (!(systemipaddress.length() > 0)) { try { InetAddress localhost = InetAddress.getLocalHost(); System.out.println((localhost.getHostAddress()).trim()); systemipaddress = (localhost.getHostAddress()).trim(); } catch(Exception e1) { systemipaddress = "Cannot Execute Properly"; } } } catch (Exception e2) { systemipaddress = "Cannot Execute Properly"; } System.out.println("\nYour IP Address: " + systemipaddress +"\n"); } } |
Output

We hope that you got a basic information about How To Find Public IP Address in Java Programming Language. In case you get any Compilation Errors with this Java Program To Check Public IP Address or you have any doubt about it, let us know about it in the Comment Section below.
Nice Program!
Thank you Shanmukha! We hope you stay connected with us and find some excellent C Programs on our website!
thank very hopefully this program bro.. but i want to automatically alert ip address .how to find that profram ..please tell that progrma
Can you explain it? Its unclear!
nice platform.. plz tell me about cryptography as well as hacking concept.
Hi,
nice program.
but i have a doubt. in my project i have an api url which used this piece of code to get public ip of a machine and that is running on lets say server A. And that api url is placed inside a separate web page which is running on server B. Now my question is… if I load the web page from different locations and different machines what will be the output of public ip?
my actual output: I am getting the public ip of server B where the web page is running.
expected output: i need to get the public ip of the particular machine on which the web page is loaded.
can anyone pls clarify it to me?
Thanks
can i get public ip address using java code (without url )