Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution to "java.net.BindException: Address already in use" error?

Tags:

I'm using Intellij, attempting to deploy a Tomcat application, but whenever I try to run it, I get the following error:

Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 1098; nested exception is:  java.net.BindException: Address already in use 

Now I've manually looked for the process happening at that port via:

lsof -i:1098 

And I found the java process and killed it too:

ps aux | grep java  kill -9 20386 

And that worked, but I have to do this EVERYTIME I open Intellij. If I want to rerun the server without exiting Intellij, I can't, because I'll get this error. Does anyone know of a permanent fix for this and not just manually killing it everytime?

like image 638
carbon_ghost Avatar asked Jun 06 '14 19:06

carbon_ghost


People also ask

What went wrong Java net BindException address already in use Cannot bind?

it means that you are trying to use a port that is already open. check to see whether the port that you want to open is already open or not. also it may cause that your fireWall do not allows the application to listen on the port.

What is Java net BindException?

java.net.BindException. Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned.

Can't start Jetty server on port address already in use BIND?

Causes. This indicates that the Jetty web server was unable to start because one of the configured ports was already in use by the operating system. The default ports used by Jetty are ports 80, 443 and 8443. If any of these ports are already in use Jetty will not start.


Video Answer


2 Answers

This is due to JMX monitoring the Tomcat instance. Tomcat will be running on port 9999 so when JMX wants to start to check for the shutdown it can't bind to this port.

One way to fix this is to define your CATALINA_OPTS environment variable.

Setting these properties in JAVA_OPTS tries to start a jmx server when you start tomcat AND when you shutdown tomcat. Hence the port already in use exception. You need to set these properties for CATALINA_OPTS instead of JAVA_OPTS. This will only run when you start tomcat.

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999" CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false" 

What fixes it for me is in $HOME/.bashrc I add this export:

export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999"

Credit: https://bowerstudios.com/node/636

like image 181
digicyc Avatar answered Sep 27 '22 18:09

digicyc


enter image description hereClick on the skull icon after stopping server to kill all associated processes. This should help in resolving the port issue.

like image 30
Gayathri Avatar answered Sep 27 '22 19:09

Gayathri