Menu
Topics Index
...
`


Exploring java.lang >
Siva Nookala - 15 Apr 2016
The abstract Process class encapsulates a process, it is an executing program.The exec() return an instance of a subclass of Process that can be used to control the process and obtain information about it.

The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying the process.
Process Contains the abstract methods as shown below.
MethodDescription
void destroy()It Kills the process.
int exit()It returns exit value obtained from subprocess.
InputStream getErrorStream()It returns the input stream connected to the error stream of the subprocess.
InputStream getInputStream()It returns the input stream connected to the output of the subprocess.
InputStream getOutputStream()It returns the output stream connected to the input of the subprocess.
int waitFor()Waits for the subprocess to complete.
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.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.