public static int findFreePort() { ServerSocket socket = null; try { socket = new ServerSocket(0); socket.setReuseAddress(true); int port = socket.getLocalPort(); closeQuietly(socket); return port; } catch (IOException e) { throw new UncheckedIoException(e); } finally { if (socket != null) { closeQuietly(socket); } } }