Sotirios-Efstathios Maneas

About 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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close