Checking if a port is in use
Don't test for port-in-use in advance. Just let your application do what it wants to do, and catch the exception there. The situation can change between the time you test and when you actually try to use the port.

Furthermore, if you are trying to write a server application, you can have Java automatically pick a free port for you.  From the JavaDoc for ServerSocket(int):


  Creates a server socket, bound to the specified port. A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range. This port number can then be retrieved by calling getLocalPort.