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