Use TCPView to resolve "Address already in use: bind" issue

When we run a runnable JAR file multiple times from the console, it's common that we would run into the following exception error:

PS C:\Users\xingzhou\Desktop\JettyApplication> java.exe -jar .\FileServer.jar
2016-11-07 13:39:28.401:INFO::main: Logging initialized @367ms
2016-11-07 13:39:28.569:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
Exception in thread "main" java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Unknown Source)
        at sun.nio.ch.Net.bind(Unknown Source)
        at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
        at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
        at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:317)
        at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
        at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:235)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at org.eclipse.jetty.server.Server.doStart(Server.java:390)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
        at org.eclipse.jetty.embedded.FileServer.main(FileServer.java:35)

The exception is obvious, that the configured port is in use by some other process. In this case, it is "8080" since we have the following code in the Main function.

public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
        // then a randomly available port will be assigned that you can either look in the logs for the port,
        // or programmatically obtain it for use in test cases.
        Server server = new Server(8080);
        
...
    }

Now, how to resolve this issue?

Firstly, we can use TCPView to check which process is listening this port.

(TCPView is a Windows program that will show you detailed listings of all TCP and UDP endpoints on your system, including the local and remote addresses and state of TCP connections.)

Here I simply kill the process 5912 to move on.

Another way to resolve this issue is using netstat command:

PS C:\Users\xingzhou\Desktop\JettyApplication> netstat -ano | findstr "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       27560
  TCP    [::]:8080              [::]:0                 LISTENING       27560
PS C:\Users\xingzhou\Desktop\JettyApplication> tasklist | findstr "27560"
javaw.exe                    27560 Console                    1     35,680 K
PS C:\Users\xingzhou\Desktop\JettyApplication> kill 27560
PS C:\Users\xingzhou\Desktop\JettyApplication> tasklist | findstr "27560"

 

posted on 2016-11-07 15:04  周星的Blog  阅读(240)  评论(0编辑  收藏  举报

导航