The below code snippet is a lovely little piece of Java magic the we use in various installation and configuration scenarios to verify if a port is available or busy

import java.io.*;
import java.net.*;</p>

<p>public class PortChecker {</p>

<pre><code>public static void main(String[] args) {

int port=0;

if (args.length == 1) {
      try {
        port = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {
        System.err.println(&amp;quot;INVALID ARGUMENT: must be an integer.&amp;quot;);
        System.exit(2);
    }
    } else {
    System.err.println(&amp;quot;A single integer argument is required&amp;quot;);
    System.exit(2);
}

try {
    ServerSocket srv = new ServerSocket(port);
    srv.close(); 
    srv = null;
    System.exit(0);
} catch (IOException e) {
    System.exit(1);
}
}
</code></pre>

<p>}