public static int findUnusedPort() throws IOException
    {
        int port;

        try (ServerSocket socket = new ServerSocket()) {
            socket.bind(new InetSocketAddress(0));
            port = socket.getLocalPort();
        }

        return port;
    }
public static int findUnusedPort() throws IOException
    {
        int port;

        try (ServerSocket socket = new ServerSocket()) {
            socket.bind(new InetSocketAddress(0));
            port = socket.getLocalPort();
        }

        return port;
    }
public static int findUnusedPort ( ) throws IOException { int port ; try ( ServerSocket socket = new ServerSocket ( ) ) { socket . bind ( new InetSocketAddress ( 0 ) ) ; port = socket . getLocalPort ( ) ; } return port ; }
Find an unused port.
Find an unused port .