Here you can find the source of getFreePort()
public static int getFreePort()
//package com.java2s; /******************************************************************************* * Copyright (c) 2011 EclipseSource and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww. j a v a2s . c o m*/ * EclipseSource - initial API and implementation ******************************************************************************/ import java.io.IOException; import java.net.ServerSocket; public class Main { public static int getFreePort() { int result; try { ServerSocket socket = new ServerSocket(0); result = socket.getLocalPort(); socket.close(); } catch (IOException ioe) { throw new RuntimeException("Failed to find free port.", ioe); } return result; } }