Here you can find the source of getAvailablePort()
public static int getAvailablePort()
//package com.java2s; /*/*from ww w.j a v a 2s .c o m*/ * Copyright (c) 2015 Youssuf ElKalay / BuildScientist. * All Rights Reserved. * This software is released under the Apache license 2.0 * This file has been modified by the copyright holder. */ import java.io.IOException; import java.net.ServerSocket; public class Main { public static int getAvailablePort() { ServerSocket serverside = null; int port = 0; try { serverside = new ServerSocket(0); port = serverside.getLocalPort(); } catch (IOException exception) { exception.printStackTrace(); } finally { try { serverside.close(); } catch (IOException exception) { exception.printStackTrace(); } } return port; } }