How to launch another program from a Java application?

The simplest and most basic way is to use the Runtime.getRuntime().exec() method. You pass the system command as a string parameter. Optionally, you can specify the working directory and environment variables as an array of strings in the format "name=value". If the command requires arguments, they can be passed either as an array or within the command string separated by spaces.

The recommended and more controlled approach is to use the ProcessBuilder class, which is also used internally by the exec method. ProcessBuilder provides tools for using pipelines and input-output redirection in the command.

Executing a command creates an instance of the Process class, which can be converted into the more modern (Java 9+) and feature-rich ProcessHandle. Through these objects, you can interact with the process's input-output, characteristics, and status.

The command runs in a separate operating system subprocess, which means the slogan "Write once, run anywhere" no longer applies here - your program becomes platform-dependent. Interacting with the OS, especially spawning a new process, usually consumes a significant amount of system resources. While launching external programs isn't considered bad practice, it's advisable to avoid it when possible.