import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {
    private static final String ENV_RESTARTED = "RESTARTED";
    private static String restarted;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            restarted = System.getenv(ENV_RESTARTED);
            if(restarted==null) {
                restarted = "0";
            }
            JFrame frame = new JFrame(restarted);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton button = new JButton("Restart this app as a child process");
            button.addActionListener((e) -> {
                try {
                    String appPath = System.getProperty("jpackage.app-path");
                    ProcessBuilder builder = new ProcessBuilder(appPath);
                    Map<String,String> env = builder.environment();
                    env.put(ENV_RESTARTED, Integer.toString(Integer.valueOf(restarted)+1));
                    builder.start();
                }
                catch(Exception ex) {
                    ex.printStackTrace();
                }
            });
            frame.setContentPane(button);
            frame.pack();
            frame.setVisible(true);
        });
    }
}