Checking if another instance of the same program is already running

The best way in my opinion is to use ServerSockets. A ServerSocket can only be opened once. So if the application is already running, the creation of the same ServerSocket will fail. This is how you know the app is already running. Sockets are the best approach because the ports get closed by the JVM upon shut down, locking files used for behavior might not work under all circumstances.
Here is a sample:

public class MainApp
{
  private static ServerSocket applLockSocket;
 
  public MainApp()
  {
    // Do something...
 
  } 
 
  private static boolean isApplRunning()
  {
    try	
    {
       // Open a ServerSocket on an app specific port
       applLockSocket = new ServerSocket(2345);  
    } 
    catch (java.net.BindException e)
    {
       //There is already an application running!
       return true;
    }
    catch (IOException e)
    {
      // This was not expected...
    }
    return false;
  } 
 
  public static void main(String[] args)
  {
    try
    {
      if (isApplRunning())
      {
        // Since we only allow this application to be 
        // started once, we need to show the user, that
        // this app is already running...		   
        JOptionPane.showMessageDialog(null
          ,"This app is already running!"
          ,"Info:"
          ,JOptionPane.INFORMATION_MESSAGE);
	System.exit(0);
      } 
 
      // Start the application...
      new MainApp();
    } 
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(null
        ,e.toString()
        ,"Error:"
	,JOptionPane.ERROR_MESSAGE);
    } 
  }
}