You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk.

For example, here's a complete program that will showcase how to do it:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class testprog {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }
}

When compiled and run, it outputs:

line: ./
line: ../
line: .classpath*
line: .project*
line: bin/
line: src/
exit: 0

as expected.

You can also get the error stream for the process standard error, and output stream for the process standard input, confusingly enough. In this context, the input and output are reversed since it's input from the process to this one (i.e., the standard output of the process).

If you want to merge the process standard output and error from Java (as opposed to using 2>&1 in the actual command), you should look into ProcessBuilder.

Answer from paxdiablo on Stack Overflow
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › linux-cmd.html
Java and the Linux Command Line
To configure Java, you will need to know which shell you are running. In case you don't know, type the following command: [wayne] ~> echo $SHELL Your shell will likely be bash, tcsh, sh, or ksh. To make sure Linux can find the Java compiler and interpreter, edit your shell login file according to the shell you are using.
🌐
TutorialsPoint
tutorialspoint.com › unix_commands › java.htm
java Command in Linux
The java command in Linux allows us to run a compiled Java application. When we execute this command, it initiates the Java Virtual Machine (JVM) in the background, loads the specified class, and invokes the main() method of that class.
🌐
Oracle
docs.oracle.com › en › java › javase › 19 › docs › specs › man › java.html
The java Command
December 12, 2022 - Removed Java Options: Removed --- using them results in an error. These are the most commonly used options supported by all implementations of the JVM. Note: To specify an argument for a long option, you can use either --name=value or --name value. ... Loads the specified native agent library. After the library name, a comma-separated list of options specific to the library can be used. Linux and macOS: If the option -agentlib:foo is specified, then the JVM attempts to load the library named libfoo.so in the location specified by the LD_LIBRARY_PATH system variable (on macOS this variable is DYLD_LIBRARY_PATH).
🌐
Javatpoint
javatpoint.com › linux-commands
50 Linux Commands List with Examples - javatpoint
50 Linux Commands List with Examples for beginners and professionals with examples on files, directories, permission, backup, ls, man, pwd, cd, chmod, man, shell, pipes, filters, regex, vi etc..
🌐
CommandLinux
commandlinux.com › home › man page › java
java
March 5, 2026 - Reports information about use of native methods and other Java Native Interface activity. ... Displays version information and exits. See also the -showversion option. ... Specifies that the version specified by the release is required by the class or JAR file specified on the command line.
Top answer
1 of 10
64

You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk.

For example, here's a complete program that will showcase how to do it:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class testprog {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }
}

When compiled and run, it outputs:

line: ./
line: ../
line: .classpath*
line: .project*
line: bin/
line: src/
exit: 0

as expected.

You can also get the error stream for the process standard error, and output stream for the process standard input, confusingly enough. In this context, the input and output are reversed since it's input from the process to this one (i.e., the standard output of the process).

If you want to merge the process standard output and error from Java (as opposed to using 2>&1 in the actual command), you should look into ProcessBuilder.

2 of 10
27

You can also write a shell script file and invoke that file from the java code. as shown below

{
   Process proc = Runtime.getRuntime().exec("./your_script.sh");                        
   proc.waitFor();
}

Write the linux commands in the script file, once the execution is over you can read the diff file in Java.

The advantage with this approach is you can change the commands with out changing java code.

🌐
Opensource.com
opensource.com › article › 21 › 10 › check-java-jps
Check Java processes on Linux with the jps command | Opensource.com
October 6, 2021 - $ ps ax |grep java 67604 pts/1 Sl+ 0:18 /usr/lib/jvm/java-11-openjdk-11.0.12.0.7-4.fc34.x86_64/bin/java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true --add-exports=java.desktop/sun.awt=ALL-UNNAMED --add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.
🌐
ArchWiki
wiki.archlinux.org › title › Java
Java - ArchWiki - Arch Linux
May 15, 2026 - archlinux-java <COMMAND> COMMAND: status List installed Java environments and enabled one get Return the short name of the Java environment set as default set <JAVA_ENV> Force <JAVA_ENV> as default unset Unset current default Java environment fix Fix an invalid/broken default Java environment configuration
Find elsewhere
🌐
Javatpoint
javatpoint.com › linux-commands-list
Linux command List - javatpoint
Linux command List with examples on files, directories, permission, backup, ls, man, pwd, cd, linux, linux introduction, chmod, man, shell, pipes, filters, regex, vi etc.
🌐
Princeton CS
introcs.cs.princeton.edu › java › linux
Hello World in Java on Linux
August 14, 2019 - To make our textbook libraries accessible to Java, use the command java-introcs instead. For example, type the following two commands to test standard drawing and standard audio: You can use Checkstyle and Findbugs to check the style of your programs and identify common bugs. To run Checkstyle, type the following command in the Terminal: Here is a list of available checks. To run Findbugs, type the following command in the Terminal: Here is a list of bug descriptions. My distribution of Linux is { Gentoo, Debian, Ubuntu, Fedora, Red Hat, SuSE, Mandriva, or Slackware }. How should I modify the instructions?
🌐
FOSS Linux
fosslinux.com › home › programming › compile and run java from the linux cli (2026 developer guide)
How to Compile and Run Java from Command-line in Linux
April 29, 2026 - To compile a Java program that references code within that JAR file, you must include it in your compilation command: fosslinux@dev:~$ javac -cp ".:external_libs/utilities.jar" Main.java · The -cp flag defines the classpath. The dot (.) represents the current directory, and the colon (:) separates multiple paths on Linux.
🌐
Sertman
sertman.com › 2011 › 12 › linux-commands-in-java.html
Linux Commands in Java
You can use getRuntime method to run linux shell commands from java. Try this example to get memory statistics: int ch; String MemStr ...
🌐
Medium
beknazarsuranchiyev.medium.com › run-terminal-commands-from-java-da4be2b1dc09
Run terminal commands from Java. In this article, we will discuss how to… | by Beknazar | Medium
April 24, 2022 - We literally do everything from the terminal in Linux to work on the server and sometimes we need to do it from the code. We are going to discuss ProcessBuilder.java and Process.java files. Ok, let’s see one code that can execute commands from the terminal: The above code executes ls commands to list files and directories on my desktop.
🌐
LinuxVox
linuxvox.com › blog › install-java-linux-command-line
Install Java on Linux via Command Line — linuxvox.com
Oracle JDK: A proprietary Java distribution provided by Oracle. It may offer additional features and support, but it also has its own license terms. Most Linux distributions use package managers to install, update, and remove software. The two most common package managers are: ... # Update the package list sudo apt update # Install OpenJDK 11 sudo apt install openjdk - 11 - jdk # If you want a different version, for example, OpenJDK 8 sudo apt install openjdk - 8 - jdk
🌐
Mkyong
mkyong.com › home › java › how to execute shell command from java
How to execute shell command from Java - Mkyong.com
January 3, 2019 - If I want to check my java version on Mac, I used the “PING” example and replaced the command string with my value i.e. String command = “java -version”; but the command does not get executed at all. Can you please let me know what am I missing? ... Mkyong your tutorials are always straight to the point, bravo. ... Hi I am trying to create a Terminal Emulator for Linux using Java…
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to check java version installed on linux
How to Check Java Version Installed on Linux
December 11, 2025 - Alternatively, use the whereis ... Java is located in /usr/bin/java. 2. List the /usr/bin/java directory content with the ls command:...
🌐
Princeton
lift.cs.princeton.edu › java › linux
Hello World in Java (Linux)
Here is a list of bug patterns. To run Checkstyle 10.26.1, type one of the following commands in the terminal, depending on whether you are COS 126, COS 226, or Coursera student: ~/hello> checkstyle -cos126 HelloWorld.java Running checkstyle on HelloWorld.java: ~/hello> checkstyle -cos226 HelloWorld.java Running checkstyle on HelloWorld.java: ~/hello> checkstyle -coursera HelloWorld.java Running checkstyle on HelloWorld.java: The argument must be a list of .java files.