The exec () method of the Runtime object can be used to obtain the Process objects of other processes. The Process object represents the sub-processes started by the Java program. The Process class provides the following three methods, used to allow the program to communicate with its child processes.
InputStream getErrorStream (): gets the error stream of the sub-process
InputStream getInputStream (): gets the input stream of the sub-process.
OutputStream getOutputStream (): gets the output stream of the sub-process.
The following code implements error output for obtaining sub-Processes
[Java]
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("adb ");
BufferedReader br = new BufferedReader (new InputStreamReader (p. getErrorStream ()));
String str = null;
While (str = br. readLine ())! = Null)
{
System. out. println (str );
}
}
}
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("adb ");
BufferedReader br = new BufferedReader (new InputStreamReader (p. getErrorStream ()));
String str = null;
While (str = br. readLine ())! = Null)
{
System. out. println (str );
}
}
}
The following program demonstrates the communication between two Java programs.
This number of parent Processes
[Java]
Import java. io. OutputStream;
Import java. io. PrintStream;
Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("java work ");
OutputStream OS = p. getOutputStream ();
PrintStream ps = new PrintStream (OS );
Ps. println ("translated ");
OS. close ();
}
}
Import java. io. OutputStream;
Import java. io. PrintStream;
Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("java work ");
OutputStream OS = p. getOutputStream ();
PrintStream ps = new PrintStream (OS );
Ps. println ("translated ");
OS. close ();
}
}
Below are the sub-Processes
[Java]
Import java. io. FileOutputStream;
Import java. io. PrintStream;
Import java. util. collections;
Public class work {
Public static void main (String [] args) throws Exception {
Pipeline SC = new pipeline (System. in );
FileOutputStream FCM = new FileOutputStream ("work ");
PrintStream ps = new PrintStream (FCM );
System. setOut (ps );
While (SC. hasNextLine ())
{
System. out. println (SC. nextLine ());
}
Ps. close ();
}
}
Import java. io. FileOutputStream;
Import java. io. PrintStream;
Import java. util. collections;
Public class work {
Public static void main (String [] args) throws Exception {
Pipeline SC = new pipeline (System. in );
FileOutputStream FCM = new FileOutputStream ("work ");
PrintStream ps = new PrintStream (FCM );
System. setOut (ps );
While (SC. hasNextLine ())
{
System. out. println (SC. nextLine ());
}
Ps. close ();
}
}