Java-WaitFor and ExitValue

WaitFor and ExitValue

The waitFor( ) method return the exit code returned by the process. This method does not return until the process from which it is called terminates. The exitValue( ) method return an exit code obtained from a subprocess.

In the following program, an object of the Runtime class is first obtained by invoking the static method getRuntime( ). An object of the Process class is also created. Subsequently, the in-built notepad is started. The main process is made to wait till the notepad finishes its functionality.

Program

fgdfgdfgfdr

Program Source

public class Javaapp {
  
    public static void main(String[] args) {
        
        Runtime rt = Runtime.getRuntime();
        Process pr = null;
        
        try {
            pr = rt.exec("notepad");
            pr.waitFor();
        }catch (Exception e)
        {
            System.out.println("Error Executing Programs");
        }
        
        if(pr.exitValue()==0)
        {
            System.out.println("Program Exit Successfully");
        }else
            System.out.println("An Error Occured");
    }
}

Leave a Comment