Java HTTP Port Find getFreePort()

Here you can find the source of getFreePort()

Description

get Free Port

License

Apache License

Declaration

public static int getFreePort() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.ServerSocket;

public class Main {
    public static int getFreePort() {
        try {//from   ww w . ja v  a 2  s  .  c om
            final ServerSocket ss = new ServerSocket(0);
            ss.setReuseAddress(true);
            final int port = ss.getLocalPort();
            ss.close();
            return port;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }
}

Related

  1. getFreePort()
  2. getFreePort()
  3. getFreePort()
  4. getFreePort()
  5. getFreePort()
  6. getFreePort()
  7. getFreePort()
  8. getFreePort()
  9. getFreePort()