Dan Dan - 4 months ago 86
Linux Question

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

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.

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


sudo initctl start tomcat


This returns a message


tomcat start/running, process 9180


But when I type in the url


my-droplet-ip-address:8080


the splash page never loads.

I have read that you can start tomcat with


sh startup.sh


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

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.

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

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 .


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.

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.

Answer

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:

find these four ports in your server.xml

  1. HTTP/1.1
  2. Tomcat Admin port.
  3. AJP/1.3
  4. SSL (if enabled).

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

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

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

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

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

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

Comments