Skip to content

Instantly share code, notes, and snippets.

@linusfoldemo
Created October 16, 2015 07:05
Show Gist options
  • Save linusfoldemo/1e1bc397da9d133e98a6 to your computer and use it in GitHub Desktop.
Save linusfoldemo/1e1bc397da9d133e98a6 to your computer and use it in GitHub Desktop.
public static int availablePort() {
int port = 0;
try (ServerSocket serverSocket = new ServerSocket(port)) {
port = serverSocket.getLocalPort();
} catch (IOException e) {
throw new IllegalStateException("Unable to get available port");
}
return port;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment