I am running a simple Java application as follows:
class MyApp 
{
    public static void main(String[] args) 
    {
        // Do stuff
    }
}
Now I want to get the process ID (PID) of this application from inside my main() function so that I can save it to a file. How can I do that in a platform-independent way?
EDIT: The existing solutions on Stackoverflow are many years old and probably better solutions exist today. This is why I'm asking this question.
EDIT 2: I would prefer solutions that do NOT require Java 9.
 
     
     
    
