How to Get the IP Address of a Website using Java GUI AWT

We are gonna learn how to get the IP address of a website using Java GUI AWT hereby. Just bring all the knowledge that you had grasped about Java GUI AWT and the URL class to the table. That’s it! You will be fine.

ip address meme for java

Without wasting any more time let’s see the example on how to get the IP address of a website:

How to Get the IP Address of a Website using Java GUI

In order to find out the IP address using Java GUI we can make use of a simple InetAddress code to extract host and then host address.

Our main code of real activity would go in actionPerformed() method:

import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class LearnAWT extends Frame { 
             TextField tf;
             Button b;

             LearnAWT() {
                       setTitle("IP Address Finder"); 
                       tf = new TextField(); 
                       b = new Button("Find IP");
                       b.setBounds(110,200,60,40); 
                       tf.setBounds(100, 100, 85, 20); 
                       add(b); 
                       add(tf);
                       setLayout(null); 
                       setSize(300,300); 
                       setVisible(true); 
                       
       b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) { 
                  try {   
                    tf.setText((InetAddress.getByName(new URL(tf.getText()).getHost())).getHostAddress()); 
                      } catch (Exception e1) { 
                        e1.printStackTrace(); 
                      } 
                   } 
                }); 
             } 
public static void main(String []args) { 
             new LearnAWT(); 
      }
}

We have simply created a frame of 300 by 300 size and set it to visible using setVisible(true) method. (It is false by default)

We have not used any existing layout. By manually providing the location of a button and a text field we have chosen our own layout. We have made use of anonymous inner classes in Java to use the addActionListener method. Then we have overriden the actionPerformed method where the main action takes place.

Hereby we have used the InetAddress class to grab the IP address of the website we plan on putting the text field. getText() and setText() are responsible for getting and putting things in the text field then.

If we run the above program we can successfully grab IP addresses from all over the web by putting the address in the text field and then clicking on the Find IP button:

IP Address Finder How to Get the IP Address of a Website

It is a piece of cake isn’t it?

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

1 Response

  1. July 10, 2017

    […] How to Get the IP Address of a Website […]

Leave a Reply