Skip to content

Instantly share code, notes, and snippets.

View domhanak's full-sized avatar
🍅
Tomato

Dominik Hanák domhanak

🍅
Tomato
  • Red Hat Inc.
  • Brno
View GitHub Profile
@vorburger
vorburger / gist:3429822
Created August 22, 2012 22:03
How to find an available (free) TCP port in Java
/**
* Returns a free port number on localhost.
*
* Heavily inspired from org.eclipse.jdt.launching.SocketUtil (to avoid a dependency to JDT just because of this).
* Slightly improved with close() missing in JDT. And throws exception instead of returning -1.
*
* @return a free port number on localhost
* @throws IllegalStateException if unable to find a free port
*/
private static int findFreePort() {