Obtaining the process information of the current JVM
A running process has a set of attributes associated with it, such as the following:
- PID: This uniquely identifies the process
- Owner: This is the name of the user who launched the process
- Command: This is the command that runs under the process
- CPU time: This indicates the time for which the process has been active
- Start time: This indicates the time when the process was launched
These are a few attributes that we are generally interested in. Perhaps we would also be interested in CPU usage or memory usage. Now, getting this information from within Java was not possible prior to Java 9. However, in Java 9, a new set of APIs has been introduced, which enables us to get the basic information about the process.Â
In this recipe, we will see how to get the process information for the current Java process; that is, the process that is executing your code.
How to do it...
Follow these steps:
- Create a simple class and use
ProcessHandle.current()
to get...