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

About Sotirios-Efstathios Maneas

Avatar photo
Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.

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


  • 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:


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);

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 ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Gideon Pereira
Gideon Pereira
2 years ago

Thanks for this explanation.