Book Image

Java 9 Cookbook

By : Mohamed Sanaulla, Nick Samoylov
Book Image

Java 9 Cookbook

By: Mohamed Sanaulla, Nick Samoylov

Overview of this book

<p>Java is an object-oriented programming language. It is one of the most widely accepted languages because of its design and programming features, particularly in its promise that you can write a program once and run it anywhere.</p> <p>This cookbook offers a range of software development examples in simple and straightforward Java 9 code, providing step-by-step resources and time-saving methods to help you solve data problems efficiently. Starting with the installation of Java, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works.</p> <p>We cover major concepts such as Project Jigsaw and various tools that will enable you to modularize your applications. You will learn new features in the form of recipes that will make your applications modular, secure, and fast.</p>
Table of Contents (22 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Spawning a new process


In this recipe, we will see how to spawn a new process using ProcessBuilder. We will also see how to make use of the input, output, and error streams. This should be a very straightforward and common recipe. However, the aim of introducing this is to make this chapter a bit more complete and not just to focus on Java 9 features.

Getting ready

There is a command in Linux called free, which shows the amount of RAM free and used by the system. It accepts an option, -m, to show the output in megabytes. So, just running free -m gives us the following output: 

We will be running the preceding code from within the Java program.

How to do it...

  1. Create an instance of ProcessBuilder by providing the required command and its options: 
        ProcessBuilder pBuilder = new ProcessBuilder("free", "-m");

An alternate way to specify the command and options is as follows:

        pBuilder.command("free", "-m");
  1. Set up the input and output streams for the process builder and other properties...