如何解决 java.net.BindException: Address already in use

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38682736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:40:11  来源:igfitidea点击:

How to solve java.net.BindException: Address already in use

javalinuxubuntu-14.04digital-oceantomcat8

提问by Dan

I am trying to set up a tomcat 8 server on a ubuntu 14 through digital ocean with this tutorial https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu-14-04. But I cannot reach the tomcat splash page.

我正在尝试使用本教程通过数字海洋在 ubuntu 14 上设置 tomcat 8 服务器https://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-8-on-ubuntu- 14-04。但是我无法访问 tomcat 启动页面。

Everything goes well until I reach the step to start tomcat with

一切顺利,直到我到达启动 tomcat 的步骤

sudo initctl start tomcat

须藤 initctl 启动 tomcat

This returns a message

这将返回一条消息

tomcat start/running, process 9180

tomcat 启动/运行,进程 9180

But when I type in the url

但是当我输入网址时

my-droplet-ip-address:8080

my-droplet-ip-address:8080

the splash page never loads.

启动页面永远不会加载。

I have read that you can start tomcat with

我读过你可以用

sh startup.sh

sh 启动.sh

Although when I do this catalina.out gives me the error message

虽然当我这样做时 catalina.out 给了我错误信息

31-Jul-2016 05:37:41.018 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
31-Jul-2016 05:37:41.086 SEVERE [main] org.apache.coyote.AbstractProtocol.init Failed to initialize end point associated with ProtocolHandler ["http-nio-8080"]
 java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:229)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:866)
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:213)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:558)
    at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:65)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:1010)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:549)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:873)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:606)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:629)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:311)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:494)

31-Jul-2016 05:37:41.089 SEVERE [main] org.apache.catalina.core.StandardService.initInternal Failed to initialize connector [Connector[HTTP/1.1-8080]]
 org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-8080]]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:111)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:549)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:873)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:606)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:629)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:311)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:494)
Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:1012)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
    ... 12 more
Caused by: java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:229)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:866)
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:213)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:558)
    at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:65)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:1010)
    ... 13 more

31-Jul-2016 05:37:41.091 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8181"]
31-Jul-2016 05:37:41.105 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
31-Jul-2016 05:37:41.117 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1568 ms
31-Jul-2016 05:37:41.180 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
31-Jul-2016 05:37:41.180 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
31-Jul-2016 05:37:41.211 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/tomcat/webapps/host-manager

I have checked to see what ports are being used with sudo lsof -i, and tried killing the processes that are using ports that tomcat needs, as well as changing the various ports in server.xml in order to avoid this error. But it keeps occuring no matter what I do.

我检查了 sudo lsof -i 正在使用哪些端口,并尝试终止使用 tomcat 需要的端口的进程,以及更改 server.xml 中的各种端口以避免此错误。但无论我做什么,它都会不断发生。

I have also recently seen this link

我最近也看到了这个链接

https://www.digitalocean.com/community/questions/tomcat-not-starting-in-ubuntu-14-04-tomcat-7-and-java-7

https://www.digitalocean.com/community/questions/tomcat-not-starting-in-ubuntu-14-04-tomcat-7-and-java-7

which suggests

这表明

This is possibly due to an entropy source problem. Set the option

-Djava.security.egd=file:/dev/./urandom either in catalina.sh or /etc/default/tomcat .

这可能是由于熵源问题。设置选项

-Djava.security.egd=file:/dev/./urandom 在 catalina.sh 或 /etc/default/tomcat 中。

The problem with this is that I cannot find the file /etc/default/tomcat, nor can I figure out where in catalina.sh to add this line that the author suggests.

这样做的问题是我找不到文件 /etc/default/tomcat,也无法弄清楚在 catalina.sh 中的何处添加作者建议的这一行。

Why can I not start tomcat to get to the splash page? Is this really an issue related to entroy source? If so, where in catalina.sh does this line go? If not, any suggestsion of how I can solve this issue would be appreciated.

为什么我不能启动 tomcat 进入启动页面?这真的是与熵源有关的问题吗?如果是这样,这条线在 catalina.sh 的什么地方?如果没有,我将不胜感激有关如何解决此问题的任何建议。

回答by Suraj

As you already know the root cause of this problem is that your application trying to connect to a particular port which is already in use by some other processes either Java or non-Java. here your answer:

正如您已经知道这个问题的根本原因是您的应用程序试图连接到一个特定的端口,该端口已经被其他一些 Java 或非 Java 进程使用。这是你的答案:

find these four ports in your server.xml

在 server.xml 中找到这四个端口

  1. HTTP/1.1
  2. Tomcat Admin port.
  3. AJP/1.3
  4. SSL (if enabled).
  1. HTTP/1.1
  2. Tomcat 管理端口。
  3. AJP/1.3
  4. SSL(如果启用)。

use netstat command to find which process is using those ports.

使用 netstat 命令查找哪个进程正在使用这些端口。

netstat -nap | grep <port>

Now since you have found o offending process you can kill that process and restart yours if killing that process is OK, otherwise, change the port in your server.xml

现在,既然您发现了有问题的进程,您可以杀死该进程并在可以杀死该进程的情况下重新启动您的进程,否则,请更改 server.xml 中的端口

do this for all 4 ports, and restart tomcat. check catalina.out, if everything goes well you will be able to access your application.

对所有 4 个端口执行此操作,然后重新启动 tomcat。检查 catalina.out,如果一切顺利,您将能够访问您的应用程序。

Is this really an issue related to entroy source? If so, where in catalina.sh does this line go?

这真的是与熵源有关的问题吗?如果是这样,这条线在 catalina.sh 的什么地方?

I don't know whether it is related to entroy source or not, but you can place -Djava.security.egd=file:/dev/./urandominto catalina.sh. find JAVA_OPTSin catalina.sh and put it there separated by semicolon(;) with existing values.

不知道是不是和熵源有关,你可以 -Djava.security.egd=file:/dev/./urandom放到catalina.sh里。JAVA_OPTS在 catalina.sh 中找到并把它放在那里,用分号(;)与现有值分开。

If yo are using Eclips IDE let me know. its all easy stuff. hope it would help.

如果您正在使用 Eclips IDE,请告诉我。它都是简单的东西。希望它会有所帮助。