Class PidFileUtil


  • public class PidFileUtil
    extends java.lang.Object
    Utility class to manage a PID file in a platform-independent manner. The class only offers two few public methods to create a PID file for the current Java process, and to check whether the process indicated by a PID file is still running.
    • Constructor Summary

      Constructors 
      Constructor Description
      PidFileUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void createPidFile​(java.io.File pidFile)
      Determines the process identifier (PID) for the currently active Java process and writes this PID to the given file.
      static int getProcessPid()
      Determines the PID for the current Java process.
      static boolean isProcessRunning​(java.io.File pidFile)
      Determines whether a process is running, based on the given PID file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getProcessPid

        public static int getProcessPid()
        Determines the PID for the current Java process.

        This is a non-trivial action, since Java does not offer an easy API. This method tries to determine the PID using two different methods getProcessPidImpl1() and getProcessPidImpl2() (if the first one fails) and returns the PID it it succeeds.

        Returns:
        The Java process PID, or -1 if the PID cannot be determined
      • createPidFile

        public static void createPidFile​(java.io.File pidFile)
                                  throws java.io.IOException
        Determines the process identifier (PID) for the currently active Java process and writes this PID to the given file.
        Throws:
        java.io.IOException
        See Also:
        getProcessPid()
      • isProcessRunning

        public static boolean isProcessRunning​(java.io.File pidFile)
        Determines whether a process is running, based on the given PID file. The method reads the PID file and then calls isProcessRunning(int). If the PID file does not exist, it returns false.