How to find IP Address of any url using Java

In this program, We will find the IP address of any URL in java. We will use many methods that get the host address of any URL. A few things are a must in this program.

  • getHostAddress
  • InetAddress

With the use of getHostAddress () we will get the Internet Protocol address of any URL in string format. InetAddress is a class that represents the Internet Protocol address.

InetAddress ia=InetAddress.getByName(url); 
String ip=ia.getHostAddress();

Get the Host name, Port no, File name, Protocol of any URL in Advance Java.




Java program to find the IP address of an URL in Swing

import javax.swing.*;  
import java.awt.event.*;  
import java.net.*;  
public class IPFinder extends JFrame implements ActionListener{  
    JLabel l;  
    JTextField tf;  
    JButton b;  
IPFinder(){  
    super("IP Finder Tool");  
    l=new JLabel("Enter URL:");  
    l.setBounds(50,70,150,20);;  
    tf=new JTextField();  
    tf.setBounds(50,100,200,20);  
      
    b=new JButton("Find IP");  
    b.setBounds(50,150,80,30);  
    b.addActionListener(this);  
    add(l);  
    add(tf);  
    add(b);  
    setSize(300,300);  
    setLayout(null);  
    setVisible(true);  
}  
public void actionPerformed(ActionEvent e){  
    String url=tf.getText();  
    try {  
        InetAddress ia=InetAddress.getByName(url);  
        String ip=ia.getHostAddress();  
        JOptionPane.showMessageDialog(this,ip);  
    } catch (UnknownHostException e1) {  
        JOptionPane.showMessageDialog(this,e1.toString());  
    }  
}  
public static void main(String[] args) {  
    new IPFinder();  
}  
} 

Steps for understanding the program: IP address in Java

  1. first, we import the packages which are mandatory for this program.
  2. we create a frame and set the size.
  3. we create a text filed, label & button on the frame.
  4. set the visibility true of all components.

The main logic of the program

 String url=tf.getText(); 
try { 
InetAddress ia=InetAddress.getByName(url); 
String ip=ia.getHostAddress(); 
JOptionPane.showMessageDialog(this,ip); 
} catch (UnknownHostException e1) { 
JOptionPane.showMessageDialog(this,e1.toString()); 
} 
}

first, we get the text from the text filed in the string type variable ‘url’. After that, we use the method of InetAddres class it will get the instance of the internet protocol address. After that, we use getHostAddress¬†to getting the IP address of the given URL.

 

The output of the program

find ip address of url in Java

 

You may also learn,


Leave a Reply

Your email address will not be published. Required fields are marked *