Skip to content
Advertisement

Create a new process in Java, exit the current process

In my code, I want to restart the program. For this i have used the following code in Windows:

if(System.getProperty("os.name").contains("Windows"))
{
    //For Windows Builds use this
    new ProcessBuilder("java", "Launcher").inheritIO().start();
    System.exit(0);
}

For Linux Builds I used

else
{
    //For Linux/Unix or Mac Builds use this
    new ProcessBuilder("/bin/bash", "-c" ,"java Launcher").inheritIO().start();
}

So now, the implementation for Windows works just fine. It begins a new instance and exits the old. But the Linux implementation is kinda a bit odd. I added System.exit(0); thinking that it will kill the current process right after creating the new one, but it seemed to exit the process itself. I cannot restart the program in anyway in Linux, although it was doable in Windows.

Would appreciate help and feedback!

EDIT: [28-July-2020]

So I did find that the new process is created, but the IO is not inherited to the new session. I tweaked a bit of code and now the program creates the new process, gets IO control and after entering a command, it exits.

if(System.getProperty("os.name").contains("Windows"))
{
    //For Windows Builds use this
    new ProcessBuilder("cmd", "/c", "java Launcher").inheritIO().start();
    System.exit(0);
}
else
{
    //For Linux/Unix or Mac Builds use this
    long pid = ProcessHandle.current().pid();
    System.out.println(pid);
    String a=String.valueOf(pid);
    Thread.sleep(10000);
    System.out.println(new ProcessBuilder("/bin/bash", "-c", "java Launcher").inheritIO().start());
    System.exit(1);
}

Without System.exit(1); the program continues with the newly created process, but with the old process still running in the background. When I try to kill the old process, both the processes are killed.

Here are the new screenshots, with the code specified above. https://gofile.io/d/MAYLeJ

EDIT: [29-July-2020]

Been working more on why the code is not working. I did get an exception for the same code, which WSL didnt detect!

The Error Log

Advertisement

Answer

Update: I did find the right answer and it might be a bit complex, but I shall try to make it as simple as possible.

In this, we will require 2 separate classes: 1 class to monitor the child process, 1 class which is the main process.

For the sake of simplicity, I shall name the monitoring class as SessionManager and the main class as mainClass

In the SessionManager class, I’ve implemented the following code.

try
{
    //A loop which will run infinitely to montior and start new processes
    while(true)
    {
        //Create the process and listen to the exit code generated by the child process spawned.
        ProcessBuilder session_monitor=new ProcessBuilder("java", "<classname>");
        Process process_monitor= session_monitor.inheritIO().start();
    
        //wait for the child process to end before proceeding
        process_monitor.waitFor();
                
        switch(process_monitor.exitValue())
        {
            //Implement the logic here
            case 0: //do something
            break;

            case 1: //do something
            break;
        }
    }
}
catch(Exception E)
{
    E.printStackTrace();
}

And in the mainClass program, we shall have a main method to start the process running.

class mainClass
{
    public static void main(String[] args)
    {
        System.out.println("Hello World");
        //exit the process with a code for the SessionManager's logic to work
        System.exit(0);          //Using a normal exit code here
    }
}

This has worked for me, and if you think this implementation can be improved, I’d love to hear it. Thank you for all your support 🙂

8 People found this is helpful
Advertisement