Executing a Subprocess

Executing a Subprocess

To be able to read from two streams, you need two threads. A common mistake is to start a process and then only read standard out (stdout. Another mistake is to read one after the other. In both cases, the process may block if it writes more than the buffer size.

Here is a class that may be used to copy one stream to another:

 /** Echoes the output from one stream into another. */  class StreamTracker implements Runnable  {    private InputStream iIn;    private OutputStream iOut;    /** Setup the streams. */    StreamTracker(InputStream inIn, OutputStream inOut)    {      iIn = inIn;      iOut = inOut;    }    /** Start echoing, proceed until eof. */    public void run()    {      try {        int lInCh = 0;        while (lInCh != -1) {          lInCh = iIn.read();          iOut.write(lInCh);        }      } catch (IOException e) {        ; // thats it      }    }  }

This clears the way for you to start a process like this:

rtime = Runtime.getRuntime();Process child = rtime.exec("/bin/bash");

The process is running and the following will track it, using our StreamTracker:

 try {  StreamTracker s1 =     new StreamTracker(child.getInputStream(), System.out);  new Thread(s1).start();  StreamTracker s2 =    new StreamTracker(child.getErrorStream(), System.out);  new Thread(s2).start();  child.waitFor(); } catch (InterruptedException e) {      ; }

In this case, the result is printed on System.out.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes