TheDeveloperBlog.com


Java ProcessBuilder Examples: Start Process, EXE

Process. When run, programs occupy their own processes. These are at the level of the operating system. And one process, in a Java program, cannot contain another one.


Starting processes. But with ProcessBuilder, in java.lang.ProcessBuilder, we construct and invoke operating system commands. We launch external processes—like EXEs.


An example program. This program creates an instance of ProcessBuilder. It then calls command() to set the command. We use two arguments: two strings.

Arguments: We pass a variable number of arguments to command—it combines these strings into a command string.

Start: With start we invoke the command. On a Windows operating system, Notepad opens the specified file.

Based on:

Java 7

Java program that uses ProcessBuilder, start

import java.io.IOException;
import java.lang.ProcessBuilder;

public class Program {
    public static void main(String[] args) throws IOException {

	// Create ProcessBuilder.
	ProcessBuilder p = new ProcessBuilder();

	// Use command "notepad.exe" and open the file.
	p.command("notepad.exe", "C:\\file.txt");
	p.start();
    }
}

Folder, EXE. In this example we invoke a specific executable at a known location on the computer. We concat a folder path and an executable name. Here I invoke photoshop.exe.

Tip: To pass an argument to the exe, specify a second argument to command. This can be a target file location to open.

Java program that uses ProcessBuilder

import java.io.IOException;
import java.lang.ProcessBuilder;

public class Program {
    public static void main(String[] args) throws IOException {

	String folder = "C:\\Program Files\\Adobe\\Adobe Photoshop CC\\";
	String exe = "photoshop.exe";

	// Create and start Process with ProcessBuilder.
	ProcessBuilder p = new ProcessBuilder();
	p.command(folder + exe);
	p.start();
    }
}

RedirectOutput. This program uses the redirectOutput method to have an executable program write to a file. It uses the 7-Zip EXE, a compression utility, and writes to "test.txt."

File: The file must exist on the disk for this example to correctly work. We can separately create the file.

Output: The program runs 7za.exe with no arguments, so the EXE does nothing of importance. We can pass further arguments to Zza.

7-Zip Command-Line
Java program that uses redirectOutput

import java.io.File;
import java.io.IOException;

public class Program {
    public static void main(String[] args) throws IOException {

	// Create ProcessBuilder and target 7-Zip executable.
	ProcessBuilder b = new ProcessBuilder();
	b.command("C:\\7za.exe");

	// Redirect output to this file.
	b.redirectOutput(new File("C:\\folder\\test.txt"));
	b.start();
    }
}

Contents: test.txt

7-Zip (A) 9.07 beta  Copyright (c) 1999-2009 Igor Pavlov  2009-08-29

Usage: 7za <command> [<switches>...] <archive_name> [<file_names>...]
       [<@listfiles...>]

ArrayList, command. The ProcessBuilder class supports an ArrayList (or any List) as an argument. We can separate parts of a command into an ArrayList and then execute them.

ArrayList

Note: This program starts Internet Explorer and opens Wikipedia in it. This is not ideal for systems that use other browsers.

Note 2: Sometimes a specific browser is standardized upon in an organization. This command would work there.

Java program that uses ArrayList for command

import java.io.IOException;
import java.util.ArrayList;

public class Program {
    public static void main(String[] args) throws IOException {

	ProcessBuilder b = new ProcessBuilder();

	// Create an ArrayList with two values.
	// ... This starts a specific browser, which is not ideal.
	ArrayList<String> values = new ArrayList<>();
	values.add("C:\\Program Files\\Internet Explorer\\iexplore.exe");
	values.add("http://en.wikipedia.org/");

	// Pass List to command method.
	b.command(values);
	b.start();
    }
}

Throws IOException. As with file handling, launching processes is prone to failure. This is not our fault. But we must protect against it.


Handling exceptions. We can specify the method throws exceptions in its declaration. Alternatively we can handle the exceptions with try and catch statements.

Exceptions

With external processes, we often can make our programs simpler. We can replace threads (which are complex) with external processes (which are isolated). This is maintainable.