Spread the love

Class Process In Java

Spread the love

Class Process In Java

The execution methods return an instance of a subclass of the process that can be used to control the process an obtain information about it.

Process class describes a process that is running externally to the Java interpreter.

Note A Process is a very different thing than a Thread. The Process class is abstract and may not be instantiated.

We need to call one of the Runtime.exec() methods to start a process and return a corresponding Process object. A Process object controls a process and gets information about it.

The Process class is an abstract class; therefore, it cannot be instantiated. The actual Process
objects created by the exec() method belong to operating-system-specific subclasses of the Process that implement the Process methods in platform-dependent ways.

waitFor() blocks until the Process exits.

exitValue() returns the exit code of the process.

destroy() kills the process. getInputStream() returns an InputStream from which we can read any bytes the process sends to its standard error stream.

getErrorStream() returns an InputStream from which you can read any bytes the process sends to its standard output stream.

getOutputStream() returns an OutputStream that you can use to send bytes to the standard input stream of the process.

NoteĀ that losing all references to a Process object, thereby making it garbage collectible, does not result in the underlying Process object dying. It merely means that there is no longer a Java object to control the process. The process itself continues to run asynchronously. In addition, no guarantees are made as to whether a controlled process will be able to continue after its parent process dies.

READ  Class DecimalFormatSymbols in Java

The abstract class structure for Process is given as

public abstract class java.lang.Process extends java.lang.Object{
//constructors
public Process(); //default constructor
//Methods
public abstract void destroy();
public abstract int exitValue();
public abstract InputStream getErrorStream();
public abstract InputStream getInputStream();
public abstract OutputStream getOutputStream();
public abstract int waitFor() throws InterruptedException;
}

The details of the class structure are given as follows:

public Process();

public Process() constructor creates a Process object.

public abstract void destroy();

public abstract void destroy() method kills the process controlled by this object.

public abstract int exitValue();

public abstract int exitValue() method returns the exit value of the process that this object is controlling.

READ  Class StringTokenizer in Java

The waitFor() method is a similar method that waits for the controlled process to terminate and then returns its exit value.

This method returns the exit value of the process controlled by this object.

public abstract InputStream getErrorStream();

public abstract InputStream getErrorStream() method returns an InputStream object that can read from the error stream of the process.

Although it is suggested that this InputStream not be buffered, the Java specification does not forbid such an implementation. In other words, although error output from programs is
traditionally unbuffered, there is no guarantee that it won’t be buffered. This means that the error output written by the process may not be received immediately.

This method returns an InputStream object connected to the error stream of the process.

public abstract InputStream getInputStream();

public abstract InputStream getInputStream() method returns an InputStream object that can read from the standard output stream of the process.

This InputStream is likely to be buffered, which means that output written by the process may not be received immediately.

READ  How to Communicate Using TCP/IP In Java

This method returns an InputStream object that is connected to the standard output stream of the process.

public abstract OutputStream getOutputStream();

public abstract OutputStream getOutputStream() method returns an OutputStream object that can write to the standard input stream of the process.

This OutputStream is likely to be buffered, which means that input sent to the process may not be received until the buffer fills up or a new line or carriage-return character is sent.

READ  Abstract Class WindowAdapter in Java

This method returns an OutputStream object that is connected to the standard input stream of the process.

public abstract int waitFor();

public abstract int waitFor() method returns the exit value of the process that this object is controlling. If the process is still running, the method waits until the process terminates and its exit value is available.

The exitValue() method is a similar method that does not wait for the controlled process to
terminate.
This method returns the exit value of the process controlled by this object.

Apart from these Process class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis of instructions,...

Comments

  1. Thanks for posting this awesome article. I’m a long time reader
    but I’ve never been compelled to leave a comment.
    I subscribed to your blog and shared this on my Twitter.
    Thanks again for a great post!

  2. Thanks for posting this awesome article. I’m a long time reader but I’ve
    never been compelled to leave a comment. I subscribed to your blog and shared this on my Twitter.
    Thanks again for a great post!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love