http://www.rgagnon.com/javadetails/java-0014.html

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Paths;

public class CmdExec {

public static void main(String args[]) {
    try {
        // enter code here

        Process p = Runtime.getRuntime().exec(
            Paths.get(System.getenv("windir"), "system32", "tree.com /A").toString()
        );

        // enter code here

        try(BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
            String line;

            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        }

    } catch (Exception err) {
        err.printStackTrace();
    }
  }
}

You can get the local path using System properties or a similar approach.

http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

Answer from James P. on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Process.html
Process (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-lang-process-class-java
Java.lang.Process class in Java - GeeksforGeeks
February 15, 2023 - Java Collection · Last Updated : 15 Feb, 2023 · The abstract Process class is a process that is, an executing program. Methods provided by the Process are used to perform input, and output, waiting for the process to complete, checking the ...
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.process api
Guide to java.lang.Process API | Baeldung
June 10, 2025 - The Process class provides methods for interacting with these processes, including extracting output, performing input, monitoring the lifecycle, checking the exit status, and destroying (killing) it.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Process.html
Process (Java SE 21 & JDK 21)
January 20, 2026 - Returns a ProcessHandle for the Process. Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) implement toHandle as the equivalent of ProcessHandle.of(pid) including the check for a SecurityManager and RuntimePermission("manageProcess").
🌐
Processing
processing.org
Welcome to Processing! / Processing.org
Download and open the 'Processing' application. Select something from the Examples. Hit the Run button. Lather, rinse, repeat as necessary. More information on using Processing itself is can be found in the environment section of the reference. To learn the Processing language, we recommend you try a few of the built-in examples, and check out the reference.
Top answer
1 of 3
38

http://www.rgagnon.com/javadetails/java-0014.html

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.file.Paths;

public class CmdExec {

public static void main(String args[]) {
    try {
        // enter code here

        Process p = Runtime.getRuntime().exec(
            Paths.get(System.getenv("windir"), "system32", "tree.com /A").toString()
        );

        // enter code here

        try(BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
            String line;

            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        }

    } catch (Exception err) {
        err.printStackTrace();
    }
  }
}

You can get the local path using System properties or a similar approach.

http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html

2 of 3
31

The Java Class Library represents external processes using the java.lang.Process class. Processes can be spawned using a java.lang.ProcessBuilder:

Process process = new ProcessBuilder("processname").start();

or the older interface exposed by the overloaded exec methods on the java.lang.Runtime class:

Process process = Runtime.getRuntime().exec("processname");

Both of these will code snippets will spawn a new process, which usually executes asynchronously and can be interacted with through the resulting Process object. If you need to check that the process has finished (or wait for it to finish), don't forget to check that the exit value (exit code) returned by process.exitValue() or process.waitFor() is as expected (0 for most programs), since no exception is thrown if the process exits abnormally.

Also note that additional code is often necessary to handle the process's I/O correctly, as described in the documentation for the Process class (emphasis added):

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

One way to make sure that I/O is correctly handled and that the exit value indicates success is to use a library like jproc that deals with the intricacies of capturing stdout and stderr, and offers a simple synchronous interface to run external processes:

ProcResult result = new ProcBuilder("processname").run();

jproc is available via maven central:

<dependency>
      <groupId>org.buildobjects</groupId>
      <artifactId>jproc</artifactId>
      <version>2.5.1</version>
</dependency>
Find elsewhere
🌐
Ongres
ongres.com › blog › java-processes-and-streams
OnGres | Java, Processes and Streams
November 24, 2020 - Still, the implementation mimics the one used by Apache Commons Exec when it comes to handle reading and writing of the process' stdin, stdout and stderr, with 3 additional threads running in background. JProc is also a modern library, supporting Java 1.7+ and provides a fluent API that is really nice to use.
🌐
DEV Community
dev.to › sadiul_hakim › java-process-api-basics-aml
Java Process API Basics - DEV Community
September 24, 2025 - The Java Process API, available since Java 5 and significantly improved in Java 9, provides a standard way to interact with native operating system processes from within a Java application.
🌐
Java Community Process
jcp.org
The Java Community Process(SM) Program
As a specification lead, you propose a new specification for the Java programming language, form an Expert Group, and shepherd that group to create a specification, Reference Implementation, and Technology Compatibility Kit.
🌐
Medium
medium.com › @ayoubtaouam › how-to-build-and-work-with-a-process-in-java-a-beginner-friendly-guide-a1172ed4d6d0
How to Build and Work with a Process in Java: A Beginner-Friendly Guide | by Ayoub Taouam | Medium
August 12, 2025 - In simple terms: A process is a program that is currently running on your computer. When you open your browser or your code editor, each one runs as its own process. Your Java application is also a process.
🌐
Android Developers
developer.android.com › api reference › process
Process | API reference | Android Developers
March 26, 2026 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
DEV Community
dev.to › dbillion › understanding-java-processes-53j0
Understanding Java Processes - DEV Community
May 5, 2024 - This code demonstrates the use of an ExecutorService to manage multiple threads that handle the input and error streams of a process. It’s a practical example of the complexity involved in process management in Java.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › class-use › Process.html
Uses of Class java.lang.Process (Java Platform SE 8 )
March 16, 2026 - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
Oracle
docs.oracle.com › cd › E17802_01 › j2se › javase › 6 › jcp › mr2 › apidiffs › java › lang › Process.html
java.lang Class Process
The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › core › creating-process.html
Creating a Process
July 11, 2024 - To create a process, first specify the attributes of the process, such as the command's name and its arguments, with the ProcessBuilder class. Then, start the process with the ProcessBuilder.start method, which returns a Process instance.
🌐
UC3M
it.uc3m.es › celeste › docencia › java › docs › api › java › lang › Process.html
Java 2 Platform SE v1.3.1: Class Process
The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
🌐
Oracle
java.com › en › download › help › windows_manual_download.html
How do I manually download and install Java for my Windows computer?
The installation process starts with appearance of Java Setup - Welcome dialog box. Click the Install button to accept the license terms and to continue with the installation.