zoukankan      html  css  js  c++  java
  • 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:UsersxingzhouDesktopJettyApplication> 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:UsersxingzhouDesktopJettyApplication> netstat -ano | findstr "8080"
      TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       27560
      TCP    [::]:8080              [::]:0                 LISTENING       27560
    PS C:UsersxingzhouDesktopJettyApplication> tasklist | findstr "27560"
    javaw.exe                    27560 Console                    1     35,680 K
    PS C:UsersxingzhouDesktopJettyApplication> kill 27560
    PS C:UsersxingzhouDesktopJettyApplication> tasklist | findstr "27560"
  • 相关阅读:
    DC中为什么要用Uniquify?
    hdu 1596 find the safest road
    hdu2112 HDU Today
    hdu 2066 一个人的旅行
    poj 3026 Borg Maze
    poj 1979 Red and Black
    poj 1321 棋盘问题
    hdu 1010 Tempter of the Bone
    hdu 4861 Couple doubi
    codeforces584B Kolya and Tanya
  • 原文地址:https://www.cnblogs.com/xingzhou/p/6038969.html
Copyright © 2011-2022 走看看