If you develop web applications on your local environment, you will notice that the port 8080 is often set as default. Most likely one day you will face a situation where 2 running web servers want to bind the same port: 8080. Obviously, this is not allowed and you will get an error “Address already in use”. In Java environment, you will get this:
[console]java.net.BindException: Address already in use[/console]
The best way to sort this conflict is to follow these simple steps.
Step 1: find if a process listen on the port 8080
Open a terminal and enter this command:
sudo lsof -i :8080 | grep LISTEN
If you have already a running server on 8080, then the command will show the application running on this port. Note that the second value column is the Process Identification Number which is automatically assigned to each process when it is created on a Unix-like operating system.
[terminal]$ lsof -i :8080 | grep LISTEN
java 28045 nicolas 125u IPv6 0x609c3697d21f8a2d 0t0 TCP *:http-alt (LISTEN)[/terminal]
Step 2: obtain more information about the process
Enter this command in the terminal.
ps -e {pid} | less
Note that you have to replace the {pid} by the value found in the result of the lsof command. In my case scenario, it’s 28045 and it’s Eclipse.
[terminal]$ ps -e 28045 | more -w
PID TTY TIME CMD
28045 ?? 0:13.70 /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=64979 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Djava.security.egd=file:/dev/./urandom -noverify -XX:TieredStopAtLevel=1 -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/resources.jar:{more libraries…}:/Users/nicolas/.m2/repository/org/springframework/spring-jcl/5.1.4.RELEASE/spring-jcl-5.1.4.RELEASE.jar com.nicolasrabier.whatever.ServerApplication –spring.output.ansi.enabled=always
[/terminal]
Step 3: kill the process
If you can no longer quit safely the web server process, there is still a way to kill the process with a command line in the terminal.
Note in the command you have to replace the {pid} by the value found in the result of the lsof command. In my case scenario, it’s still 28045.
sudo kill -9 {pid}
After running this command, no process listens on the port 8080 which means that you can start safely the other web server.