Here you can find the source of getFreePort()
public static int getFreePort()
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.net.ServerSocket; public class Main { /**// w w w.java2s . co m * tries to get a free port and returns it * @return the free port , -1 if no port */ public static int getFreePort() { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(0); int freePort = serverSocket.getLocalPort(); return freePort; } catch (IOException e) { return -1; } finally { try { serverSocket.close(); } catch (IOException e) { //do nothing } } } }