Search code examples
javapid

Is it possible to create an instance of Process class if I have pid? Java


Is it possible to create an instance of Process class if I have pid of process which already running? And put this Process in Map processMap. And then be able to stop Processes or check IsAlive


Solution

  • This isn't what Process is for:

    Process provides control of native processes started by ProcessBuilder.start and Runtime.exec.

    That's not to say you can't control already-running processes from Java; it's just that Process isn't the thing you should use to do it.

    You can make your own class to do what you say you need, e.g:

    interface ExternalProcess {
      boolean isRunning();
    
      void kill();
    }
    

    with implementations of the methods such as: