Get a Free Grip on the Output of the Process Object

Get a Free Grip on the Output of the Process Object

There are times when you have to use the exec(…) method of java.lang.Runtime class to launch a program. For example:

 public void some_method()...String[] commandArray = new String[1];commandArray[0] = "some_program";java.lang.Runtime rt = Runtime.getRuntime();java.lang.Process p = rt.exec(commandArray);...

The outputs of the program you just launched using java.lang.Runtime.exec(…) are directed to your execution context and not to the system console–in other words, the Process object (referenced by “p”) is receiving the output. Now, you can get a handle on the outputs of your program through:

 InputStream stderr = p.getErrorStream( );InputStream stdout = p.getOutputStream( );

Next, you can poll the InputStream objects you created and get the desired output, but if you do this inside the same thread that exec(..)’ed commandArray, you’ll need to block till Process object p terminates, which may not be desirable.

 ...}//end some_method

To avoid this problem, and let some_method() run the rest of its own code and exit, you can take the polling of the outputs of Process object p on separate threads as follows:

 public class SomeClass{...public void some_method()...String[] commandArray = new String[1];commandArray[0] = "some_program";java.lang.Runtime rt = Runtime.getRuntime();java.lang.Process p = rt.exec(commandArray);new OutputPollster(p.getErrorStream()).start();new OutputPollster(p.getOutputStream()").start();...}//end some_methosclass OutputPollster extends java.lang.Thread{private java.io.InputStream m_is;OutputPollster(java.io.InputStream is) {m _is = is;}public void run(){int len;byte buf[] = new byte[128];try{while (-1 != (len = _is.read(buf))){System.out.println(new String(buf));}}catch (java.io.IOException ex){//deal with the exception}}}//end OutputPollster definition }//end SomeClass definition
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

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing