import java . net . ServerSocket ; public class TestNetUtil { public static void main ( String [] args ) { try { ServerSocket socket = new ServerSocket ( 0 ); int port = socket . getLocalPort (); socket . close (); } catch ( Exception e ) {
public class TestNetUtil {
public static void main(String [] args) {
try {
ServerSocket socket = new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
执行javac TestNetUtil.java,会生成TestNetUtil.class
然后执行java TestNetUtil