try:

ps aux | grep java

and see how you get on

Answer from Rich on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › how to run a shell command in java
How to Run a Shell Command in Java | Baeldung
January 8, 2024 - In this article, we’ll learn how to execute a shell command from Java applications. First, we’ll use the .exec() method the Runtime class provides. Then, we’ll learn about ProcessBuilder, which is more customizable.
Top answer
1 of 15
166

One way to run a process from a different directory to the working directory of your Java program is to change directory and then run the process in the same command line. You can do this by getting cmd.exe to run a command line such as cd some_directory && some_program.

The following example changes to a different directory and runs dir from there. Admittedly, I could just dir that directory without needing to cd to it, but this is only an example:

Copyimport java.io.*;

public class CmdTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && dir");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}

Note also that I'm using a ProcessBuilder to run the command. Amongst other things, this allows me to redirect the process's standard error into its standard output, by calling redirectErrorStream(true). Doing so gives me only one stream to read from.

This gives me the following output on my machine:

C:\Users\Luke\StackOverflow>java CmdTest
 Volume in drive C is Windows7
 Volume Serial Number is D8F0-C934

 Directory of C:\Program Files\Microsoft SQL Server

29/07/2011  11:03    <DIR>          .
29/07/2011  11:03    <DIR>          ..
21/01/2011  20:37    <DIR>          100
21/01/2011  20:35    <DIR>          80
21/01/2011  20:35    <DIR>          90
21/01/2011  20:39    <DIR>          MSSQL10_50.SQLEXPRESS
               0 File(s)              0 bytes
               6 Dir(s)  209,496,424,448 bytes free
2 of 15
19

You can try this:-

CopyProcess p = Runtime.getRuntime().exec(command);
🌐
Mkyong
mkyong.com › home › java › how to execute shell command from java
How to execute shell command from Java - Mkyong.com
January 3, 2019 - The Process.waitFor() statement should instead be invoked after the BufferedReader finishes reading the input stream of the Process. This fixes an issue I am having here: the program stucks and never returns but the actual command in my Mac Terminal exits within a second. Another tip is that it is better to use Process.waitFor(long, TimeUnit) to prevent that the Java program hangs.
🌐
Oracle
docs.oracle.com › en › java › javase › 13 › docs › specs › man › jcmd.html
The jcmd Command
Prints the performance counters exposed by the specified Java process. ... Reads and executes commands from a specified file, filename.
🌐
Medium
harith-sankalpa.medium.com › how-to-run-system-commands-from-java-applications-a914223edd24
How To Run System Commands From Java Applications | by Harith Sankalpa | Medium
October 21, 2019 - In Java, Runtime class cannot be instantiated explicitly. But you can obtain a Runtime object using, ... After obtaining a Runtime object, you can run a system command by either passing a complete system command with arguments as a one-string or an array of strings with the command and each argument as separate strings to the exec method. As soon as the “exec” method is called the input command will be run. exec() method returns a Process object which can be used for the next step.
🌐
Stack Abuse
stackabuse.com › executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - We've used the Runtime and ProcessBuilder classes to do this. Using Java, we can run single or multiple shell commands, execute shell scripts, run the terminal/command prompt, set working directories and manipulate environment variables through core classes.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › guide to java.lang.process api
Guide to java.lang.Process API | Baeldung
June 10, 2025 - We can now obtain a lot of information about the process via the Java API java.lang.ProcessHandle.Info: ... private static void infoOfCurrentProcess() { ProcessHandle processHandle = ProcessHandle.current(); ProcessHandle.Info processInfo = processHandle.info(); log.info("PID: " + processHandle.pid()); log.info("Arguments: " + processInfo.arguments()); log.info("Command: " + processInfo.command()); log.info("Instant: " + processInfo.startInstant()); log.info("Total CPU duration: " + processInfo.totalCpuDuration()); log.info("User: " + processInfo.user()); }
Top answer
1 of 8
216
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

2 of 8
61

You can also watch the output like this:

final Process p = Runtime.getRuntime().exec("java -jar map.jar time.rel test.txt debug");

new Thread(new Runnable() {
    public void run() {
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;

        try {
            while ((line = input.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

p.waitFor();

And don't forget, if you are running a windows command, you need to put cmd /c in front of your command.

EDIT: And for bonus points, you can also use ProcessBuilder to pass input to a program:

String[] command = new String[] {
        "choice",
        "/C",
        "YN",
        "/M",
        "\"Press Y if you're cool\""
};
String inputLine = "Y";

ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

writer.write(inputLine);
writer.newLine();
writer.close();

String line;

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

This will run the windows command choice /C YN /M "Press Y if you're cool" and respond with a Y. So, the output will be:

Press Y if you're cool [Y,N]?Y
🌐
Igor's Blog
igorkromin.net › index.php › 2019 › 05 › 12 › how-to-get-the-full-command-line-used-to-start-a-java-process-in-linux
How to get the full command line used to start a Java process in Linux | Igor Kromin
cat /proc/<PID>/cmdline Luckily the JDK has a solution for us. The jinfo command can be used to extract the full command line (minus the executable path) for the JVM process...
🌐
CodeJava
codejava.net › java-se › file-io › execute-operating-system-commands-using-runtime-exec-methods
How to Execute Operating System Commands in Java
July 27, 2019 - Throughout this tutorial, you will learn how to execute a native command from within a Java program, including sending inputs to and getting outputs from the command.Basically, to execute a system command, pass the command string to the exec() method of the Runtime class.
🌐
Medium
mustafa-aydogan.medium.com › how-to-run-a-command-from-java-fe6b942b98a
How to Run a Command from Java. Sometimes, you may want to run a… | by Mustafa Aydoğan | Medium
September 27, 2025 - The ProcessBuilder class is part of the java.lang package and allows you to create and start a new process. You can use this class to execute any command that can be run from the command line, including shell commands, system commands, and external programs.
🌐
Medium
medium.com › @AlexanderObregon › processing-command-line-arguments-in-java-applications-200ba9b7f029
Processing Command-Line Arguments in Java Applications
March 14, 2025 - Learn how Java processes command-line arguments, converts them into different data types, and uses them to configure applications dynamically at runtime.
🌐
Opensource.com
opensource.com › article › 21 › 10 › check-java-jps
Check Java processes on Linux with the jps command | Opensource.com
October 6, 2021 - The Java Virtual Machine Process Status (jps) tool allows you to scan for each running instance of the Java Virtual Machine (JVM) on your system. To view a similar output as seen in the ps command, use the -v option.
🌐
Jfeatures
jfeatures.com › blog › jps
Find all the Java processes running on your machine
May 28, 2021 - For rest of the blog post we will use jps on this process. java -XX:ConcGCThreads=6 -Xmx256m -Xms8m -Xss256k Test argument1 argument2 · Following command shows process ids.
🌐
Alvin Alexander
alvinalexander.com › java › edu › pj › pj010016
Running system commands in Java applications | alvinalexander.com
June 4, 2016 - Executing a system command is relatively simple - once you've seen it done the first time. It involves the use of two Java classes, the Runtime class and the Process class. Basically, you use the exec method of the Runtime class to run the command as a separate process.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › tools › jinfo.html
jinfo - Java
April 12, 2024 - To get a list of Java processes running on a machine, use either the ps command or, if the JVM processes are not running in a separate docker instance, the jps command.