try { ServerSocket httpd = new ServerSocket(0); System.out.println("This server runs on port " + httpd.getLocalPort()); } catch (IOException e) { System.err.println(e); }