Getting the IP Address of a workstation by using Java

Getting the IP Address of a workstation by using Java

How can I simply get the IP address of a workstation by using Java so that I can display it and run another application ?

The class allows Java programs to do this.Given a host name, one can use the getByName method to find out itsIP address:

InetAddress address = InetAddress.getByName(“”);	byte[] IPnumber = address.getAddress();
Note that getByName is a static method and therefore can be called withouthaving an InetAddress object to begin with. This is similar to how theSystem.out.println() method works.

Also note that getByName() returns an InetAddress object that can thenbe used to find out the individual pieces of the IP address by getting an arrayof bytes.

You should also know that applets are not allowed to perform generalIP address look-ups. They can only look up the IP address of the user’slocal system and the server system they came from. This is a security measurethat prevents malicious applets from performing IP address look-upsinside a protected intranet and shipping that information back out tothe Internet.

The following NameLookup program can be run as either an applet or astandalone Java application. As an applet it con only look up thelocal system’s IP address and that of the server. As an application,it can look up arbitrary IP addresses.

To invoke it as an application,do:

java NameLookup———————————————————————————-import java.applet.*;import java.awt.*;import*;import*;public class NameLookup extends Applet {	TextField hostname;	Label addressLabel;	public void init() {		setLayout(new BorderLayout());		setBackground(Color.white);		Panel namePanel = new Panel();		Label nameLabel = new Label(“Host name: “);		namePanel.add(nameLabel);		hostname = new TextField(20);		namePanel.add(hostname);		add(“North”, namePanel);		addressLabel = new Label(“IP address: “);		add(“Center”, addressLabel);		Label selfLabel = new Label();		add(“South”, selfLabel);		try {			InetAddress address = InetAddress.getLocalHost();			byte[] IPnumber = address.getAddress();			// The reason for doing the bitwise AND is to			// make sure the bytes are read as unsigned integers			selfLabel.setText(“Local IP address: ” + 					(0xFF & IPnumber[0]) + “.” +					(0xFF & IPnumber[1]) + “.” +					(0xFF & IPnumber[2]) + “.” +					(0xFF & IPnumber[3]));		} catch (Exception e) { 			selfLabel.setText(“Unable to determine ” +						“local IP address”);		}	}	public synchronized boolean action(Event e, Object what) {		if ( == hostname && == Event.ACTION_EVENT) {			try {				InetAddress address =  					InetAddress.getByName(hostname.getText());				byte[] IPnumber = address.getAddress();				addressLabel.setText(“IP address: ” + 					(0xFF & IPnumber[0]) + “.” +					(0xFF & IPnumber[1]) + “.” +					(0xFF & IPnumber[2]) + “.” +					(0xFF & IPnumber[3]));			} catch (Exception err) {				addressLabel.setText(“Unable to” +					“determine IP address”);			}			return true;		} 		return false;	}	public static void main(String argv[]) {		Frame f = new Frame();		NameLookup a = new NameLookup();		a.init();		a.start();		f.setLayout(new BorderLayout());		f.add(“Center”, a);		f.pack();;	}}

See also  5 Ways to Improve Customer Experience

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist