Detect closing another sketch

Note that this is outside the PDE, you need vscode / intelliJ / eclipse.

My goal is to start a sketch, run that, and when it is closed either by me or some timer or whatever, run another sketch.
For a sketch to run I create a object of it first.
This is wrong cause it is supposed to be called static but then I will have no reference.

After that I wait in the while loop to get the PSurface, but it seems this always stays null for some reason.

This is what I have now:

  public static void main(String[] args) {
  
        SomeSketch c = new SomeSketch(); 
        c.main(args);

        PSurface s = null;
        
        while (s == null) {
            s = c.getSurface();
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        println("!");
    }

Running the sketch goes fine but I can never detect it being closed, or even get into the while loop.

I hope someone can help.

Hi, I haven’t tried this yet, but Java 9 improved a lot the Process API. See https://www.javaworld.com/article/3176874/java-language/java-9s-other-new-enhancements-part-3.html
At the end it mentions “Triggering actions on process termination”. Would this help you?