Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to get IP of any website with Java

In this article we will learn how to get the IP address of any website using Java Programming. We will use GUI, Network classes of Java Programming language with swing class.

Introduction

Working with GUI in Java Application is a beautiful technique, because it’s on you how to fetch the data on the GUI mode.

Java Programming IDE:

For this Application of Java we will use netbeans IDE, because I have experience on Netbeans IDE, but you can use any other IDE as eclipse or Dr.Java according to your experience. It is also simple to create a project on Eclipse as we are now going to create a project on Netbeans 7.3 IDE.

Creating a Project on Netbeans IDE:

Opening Netbeans 7.3 IDE

Figure 1: Opening Netbeans 7.3 IDE

Right-click on the Netbeans IDE Icon and select Run as administrator, then there will be a message box click on yes after that you’re Netbeans IDE will be open according to (Figure 1). It is not necessary to Right-click on the Netbeans IDE, you can open it from the start menu and then use the Programs list to click on the Netbeans IDE name to open it.

Netbeans 7.3 IDE is now fully ready to create a Project for any Java Application

Figure 2: Netbeans 7.3 IDE is now fully ready to create a Project for any Java Application

In (Figure 2) we will click on File menu from the menu bar and then select New Project option, we can also use the shortcut keys for this option as Ctrl+Shift+N, then there will be a new window in front of you. From that window we will choose the project for this JavaIPApplication. As a software developer we should remember the short keys of any application and we know there is a standard of short keys so it is not difficult job to remember the short keys.

Select a new Project Type, using Categories and Projects

Figure 3: Select a new Project Type, using Categories and Projects

On this window (Figure 3), we will select Java from the Categories Option, Java Application option from the Projects option. After selecting the given options click on Next button. There are many other options in two lists (Projects & Categories) but for now this application we just work on first options from both lists.

New Java Application window, we will give a name to our application

Figure 4: New Java Application window, we will give a name to our application

In (Figure 4), give a name to your project as I gave it “JavaIPApplication”, then do nothing and click on Finish button. And most important thing if you want to change the class name of this application so you can do it now, because commonly we save the Java Applications by the main class name. And also you can uncheck the last option which is create Main Class. If you uncheck create Main class option so you will create the class your own, it will not create automatically by Netbeans 7.3 IDE.

Ready to start writing the codes of Java Programming for JavaIPApplication

Figure 5: Ready to start writing the codes of Java Programming for JavaIPApplication

In (Figure 5) as you can see it, Netbeans 7.3 IDE is fully ready to start the Java Programming. First of all we will import some class’s objects which we need in this application. As you can see in (Figure 5) some Java Programming codes are automatically written, it is due to that when we checked the option in (Figure 4) create Main class option.

Listing 1: Import Java Classes

import javax.swing.JOptionPane;
import java.net.*;

By importing these two things in our project, from using the “import javax.swing.JOptionPane;” we will design our application as GUI mode. And from using the “import java.net.*;” we will check the IP address of any website.

So let’s start programming using the Java Programming language.

Listing 2: Java Programming Code for JavaIPApplication.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaipapplication;
//importing the swing class objects to use the JOptionPane for the GUI mode
import javax.swing.JOptionPane;
//importing the Network class to get the IP addressing by using the InetAddress Class
import java.net.*;
/**
 *
 * @author FAISAL ABDULLAH
 */
public class JavaIPApplication {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
        // TODO code application logic here  
                String WebsiteName = JOptionPane.showInputDialog("Enter Website Name To get its IP:");
//declaring variable for taking input from the user
    try{
//declaring Object as address of InetAddress class to get the IP address of the given website name
      InetAddress address = InetAddress.getByName(WebsiteName);
      JOptionPane.showMessageDialog(null,"The IP Address is "+address,"JavaIPFiner",JOptionPane.PLAIN_MESSAGE);
//Using JOptionPane to show a message as an output with the found IP address of the website
    }catch(UnknownHostException e){
      JOptionPane.showMessageDialog(null,"Sorry!!! Could not found the IP of "+WebsiteName,"JavaIPFinder",JOptionPane.PLAIN_MESSAGE);
//Using JOptionPane to show a message as an output when failed to find the IP address of the given website
    }
    }
}

Running the Application:

Once we designed the JavaIPApplication, then run or execute it.

JavaIPApplication execution and asking user to input website name

Figure 6: JavaIPApplication execution and asking user to input website name

Now as you can see (Figure 6), we executed our JavaIPApplication and it is asking us to enter the name of website whose IP address you want to know. So let’s we try to input the www.google.com to get its IP address, for our practice.

Ip Check

Figure 7: www.google.com as given input for IP check

In (Figure 7) as we gave the www.google.com as input, when we press the OK button then it will show the IP address of Google, and after getting the IP address of Google we can visit the Google website by using its IP address. So let we press the OK button to get its IP address.

JavaIPApplication is working and shows the IP of Google

Figure 8: JavaIPApplication is working and shows the IP of Google

As in (Figure 7) we pressed the OK button it is now showing the IP address of Google in (Figure 8). Now if we use the IP address which is in (Figure 8). So let’s we try to visit the Google website by using the IP address 173.194.35.113.

Accessing the Google website by using its IP address

Figure 9: Accessing the Google website by using its IP address

So we done it, it’s a great achievement by Java Programming, as you see in (Figure 9) we accessed the Google website by using the IP address. You can use other websites to get their IP address and access them by their IP address not by their domain name.

showing Localhost web server IP address

Figure 10: showing Localhost web server IP address

But when you execute this JavaIPApplication and if you don’t give it any website name and directly press the OK button, it will show the localhost IP address. As show in (Figure 10), by which we can access the Localhost web server by its IP address which is 127.0.0.1 according to the JavaIPApplication.

There are many other beautiful techniques in Java Programming which are very helpful to use them and create different type of helpful applications in Java Programming. I hope you will use this application for you education purposes, don’t try to tease any one.

Description of the Java Application:

As we used many things in this application to check the IP address of any website. For example we imported and also used Network classes and objects. Which are given below:

Listing 3: Details of Code

6 import javax.swing.JOptionPane;
7 import java.net.*;
24 InetAddress address = InetAddress.getByName(WebsiteName);

These three lines 6, 7 and 24 are very important to understand, why we used them in this application? On line number 6 we imported the swing class JOptionPane by which we worked with GUI (Graphical User Interface), and line 7 we imported the Network class of Java Programming Language by which we used the InetAddress class on line 24.

And in line number 22 we declared a string variable as “WebsiteName”, and then we used the JOptionPane class to get the input from the user by the help of object “showInputDialoge()” i.e.

22 String WebsiteName = JOptionPane.showInputDialog("Enter Website Name To get its IP:");

And in line number 24 we used the Network class that is InetAddress and then we created an object then we named the object as “address”. You can change its name as you want.

After this we used a member function of the class InetAddress as getByName(), and we send our declared variable on line number 22 WebsiteName as an argument to the member function getByName(), of the InetAddress. i.e.

24 InetAddress address = InetAddress.getByName(WebsiteName);

When did these all things we used the JOptionPane member function showMessageDialog() to output the IP address if it is found. Because we also used Exception Handling for this purpose i.e. try{} and catch(){}.

When it found the IP of given website so it will show the IP address in the output if it didn’t get the IP address then it will show the message from the catch(){} handling.

Conclusion:

We learnt in this article how to get the IP address of any website using the Java Programming. And also we learnt the usage of Java programming language classes in this application. I hope you enjoyed this article and learnt something from it.



Studing Computer Science and have knowledge in C/C++, Java, VB.net, C#, Assembly Programming, MFC, HTML, XML, JAVASCRIPT, AJAX, JQUERY, PHP, MYSQL, ORACLE

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login