The Tomcat java.net.BindException: Address Already in Use Error in Java – Tomcat java.net.BindException:Java中的地址已在使用中的错误

最后修改: 2018年 5月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll look at what causes the common java.net.BindingException Error: Address already in Use error and how we can deal with it.

在这个快速教程中,我们将看看是什么导致了常见的java.net.BindingException Error。地址已在使用中错误以及我们如何处理它。

2. When Does the Error Occur?

2.错误何时发生?

As we know, the Apache Tomcat server, by default, uses the 8080 port.

正如我们所知,Apache Tomcat服务器,默认情况下,使用8080端口。

Port numbers range from 0 to 65535, however, a port can only be occupied by a single application at any time.

端口号范围从0到65535,然而,一个端口在任何时候都只能被一个应用程序占用

The exception states that the application is trying to use a port that’s already taken by some other process, or we didn’t stop the Tomcat server properly.

异常说明应用程序正试图使用一个已经被其他进程占用的端口,或者我们没有正确地停止Tomcat服务器。

3. Diagnosing

3. 诊断

To solve this error, we can either kill the service taking that port or can change our web server to run on another port.

为了解决这个错误,我们可以杀死占用该端口的服务,或者可以改变我们的网络服务器,使其在另一个端口上运行。

3.1. Discovering the Conflict

3.1.发现冲突

In this case, we need to find out which application is using the port.

在这种情况下,我们需要找出哪个应用程序正在使用该端口。

The netstat command can be used for discovering current TCP/IP connections.

netstat命令可用于发现当前的TCP/IP连接。

Below are the commands that can be used to find and kill the process in different environments.

下面是在不同环境下可以用来查找和杀死进程的命令。

On Windows, the last column of output will give us the process id of the service currently running on 8080:

在Windows上,输出的最后一列将给我们提供当前在8080上运行的服务的进程ID。

netstat -ano | find "8080"

Output:

输出。

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 21376

Here, 21376 is the process id of the process which is listening on port 8080.

这里,21376是进程的ID,该进程正在监听8080端口。

On Unix/Linux Environment:

在Unix/Linux环境下

netstat -pant | grep "8080"

Output:

输出。

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 21376

Same as Windows output. Here, 21376 is the process id of the process which is listening on port 8080.

与Windows的输出相同。这里,21376是监听8080端口的进程ID。

On Mac OS X:

在Mac OS X上:

lsof -t -i :8080

Output:

输出。

21376

It will display the PID only.

它将只显示PID。

3.2. Running Server on Another Port

3.2.在另一个端口上运行服务器

If we know what process is running, why it’s running and that it needs to be running on that port, we can change the port that our server application is attempting to run on.

如果我们知道什么进程在运行,为什么它在运行,以及它需要在那个端口上运行,我们就可以改变我们的服务器应用程序试图运行的端口。

To change the Tomcat port, we need to edit the server.xml file. To do this:

要改变Tomcat的端口,我们需要编辑server.xml文件。要做到这一点。

  • Open tomcat/conf folder
  • edit server.xml
  • replace the connector port with the new port
  • restart tomcat server

The server.xml file looks like this:

server.xml文件看起来像这样。

<Connector port="8080" protocol="HTTP/1.1" 
  connectionTimeout="20000" redirectPort="8443" />

Now Tomcat will run on the customized port.

现在Tomcat将在自定义的端口上运行。

3.3. Killing the Running Service

3.3.杀死正在运行的服务

To stop the running process, we can use the kill command. 

要停止运行的进程,我们可以使用kill命令。

Using the process ID that we found in 3.1., we’ll require different commands depending on the Operating System we’re running.

使用我们在3.1中发现的进程ID,我们将根据我们运行的操作系统需要不同的命令。

On Windows environment:

在Windows环境下:

taskkill /F /PID 21376

On Unix/Linux environment:

在Unix/Linux环境下:

kill - 21376

Mac OS X environment:

Mac OS X环境:

kill -9 21376

4. Conclusion

4.总结

As mentioned at the beginning of the article, java.net.BindingException is a prevalent but easily resolved error.

正如文章开头提到的,java.net.BindingException是一个普遍存在但容易解决的错误。

The main difficulty is in finding the conflicting service using the port with the netstat terminal application then deciding the appropriate course of action.

主要的困难是用netstat终端应用程序找到使用该端口的冲突服务,然后决定适当的行动方案。

Once discovered, the fix is easy.

一旦发现,修复很容易。