Menu
Topics Index
...
`

ProcessDemo - Java Example Program


ProcessDemo
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class ProcessDemo
{
    public static void main(String args[]) throws IOException, InterruptedException
        {
        try{
        
        Runtime rt=Runtime.getRuntime();//Line A
        Process Ispro=rt.exec("notepad.exe");//Line B
        InputStream in= (InputStream) Ispro.getInputStream();//Line C
        Ispro.destroy();//Line D
        System.out.println(Ispro.exitValue());//Line E
    
        
        }
        catch (Exception ex) {
            ex.printStackTrace();
         }
        try{
            
            Runtime rt=Runtime.getRuntime();//Line F
            Process pro=rt.exec("notepad.exe");// Line G
            OutputStream out= (OutputStream)pro.getOutputStream();//Line H
            System.out.println("good");
            System.out.println("bad");
            out.close();
            }
        catch (Exception ex) {
            ex.printStackTrace();
         }
        
        
    }
}
OUTPUT

1
good
bad

DESCRIPTION

Here,in the above example program Line A,Line B represents how to create Process in java.Here exec() is used to control the process and obtain the information about it.In Line C we can get InputStream of the subprocess.In Line D we can Terminate the process,in Line E we can print the exit value of subprocess.In Line F and Line G we can create another process.In Line H we can get the OutputStream of the subprocess, then we can print the two lines of code.

THINGS TO TRY
If we execute this program in our desktops it will display a notepad window and the remaing good,bad code will be print in the command promt.

© meritcampus 2019

All Rights Reserved.

Open In App