Home » Java Basics » exceptions » java.net.BindException – How to handle BindException

About Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

java.net.BindException – How to handle BindException

In this example we will discuss about BindException in Java. This exception is thrown to indicate that an error occurred when an application attempts to bind a socket to a local address and port. The main cause of this exception is that either the port is already in use, or the requested address cannot be assigned to the calling application.

The BindException class extends the SocketException class, which is thrown to indicate an error while creating or accessing a Socket. In addition, the SocketException class extends the IOException class, which is used to indicate that an I/O exception has been occurred.

Finally, the BindException exists since the 1.1 version of Java.

The Structure of BindException

Constructors

  • BindException()
  • Creates an instance of the BindException class, setting null as its message.

  • BindException(String s)
  • Creates an instance of the BindException class, using the specified string as message. The string argument indicates the name of the class that threw the error.

The BindException in Java

As we already explained, the BindException is thrown when your Java application tries to bind a socket to a local address and port, but they are being used by another application. Take a closer look to the following example:

BindExceptionExample.java:

import java.io.IOException;
import java.net.ServerSocket;

public class BindExceptionExample {
	
	private final static int PORT = 15000;
	
	public static void main(String[] args) throws IOException {
		ServerSocket _socket = new ServerSocket(PORT);
		
		//The following statement throws a BindException.
		ServerSocket _socket_ = new ServerSocket(PORT);
		
		_socket.close();
		_socket_.close();
	}
}

In this example we want to create two instances of the ServerSocket class. The first instance is successfully created and binds the specified port. When the second instance is about to be created, using the same port, then, a BindException is thrown.

A sample execution is shown below:

Exception in thread "main" java.net.BindException: Address already in use
	at java.net.PlainSocketImpl.socketBind(Native Method)
	at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382)
	at java.net.ServerSocket.bind(ServerSocket.java:375)
	at java.net.ServerSocket.(ServerSocket.java:237)
	at java.net.ServerSocket.(ServerSocket.java:128)
	at main.java.BindExceptionExample.main(BindExceptionExample.java:14)

The simplest way to avoid this exception is to use another port and verify that the port is not in use by another application.

 
This was a tutorial about BindException in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.