Java Snippets

Hamburger icon

ProcessBuilder Uppercase

This is an example of the use of Java's ProcessBuilder class. This example is called, and it creates a child process that communicates with the parent in a bidirectional way using streams.

The parent process reads lines from its standard input and sends them to the standard input of the child process, using the OutputStream of the parent. The child process then converts it into uppercase, and prints it through its standard output. Then the parent prints what it receives from the child's InputStream and prints it too through its standard output.

The child process

The child process runs the program uppercase.c. Compile this program first using:

$ gcc uppercase.c -o uppercase

then check it works by running it:

$ ./uppercase
The code of uppercase.c is:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {

	char text[1024];
	scanf("%s", &text[0]);

	int i;
	for(i = 0; i < strlen(text); i++) {
		text[i] = toupper(text[i]);

	printf("%s\n", text);
	return 0;

The parent process

The class creates a system process using ProcessBuilder, and takes as argument the command ./uppercase. This class doesn't use Commons, so you can just compile it like this:

$ javac

Once compiled, you can run it like this:

$ java PBUppercase ./uppercase

Here is the code of PBUppercase:

import java.util.Scanner;



public class PBUppercase {
	public static void main(String[] command) {
		String textIn, textOut;
		Scanner sc = new Scanner(;

		try {
			System.out.println("\nWrite something in lowercase and I'll convert it into uppercase: ");
			textIn = sc.nextLine();

			Process process   = new ProcessBuilder(command).start();
			OutputStream os   = process.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));


		 	InputStream is    = process.getInputStream();
			BufferedReader br = new BufferedReader (new InputStreamReader(is));

			while ((textOut = br.readLine()) != null) {
				System.out.println(textOut + "\n");


		} catch (IOException e) {
			System.out.println("\nThere was an error running your command. Description:" + e.getMessage() + "\n");
		} catch (Exception e) {
			System.out.println("\nThere was a general error:" + e.getMessage() + "\n");
View PB Uppercase's repo