You need to run it using bash executable like this:

Runtime.getRuntime().exec("/bin/bash -c your_command");

Update: As suggested by xav, it is advisable to use ProcessBuilder instead:

String[] args = new String[] {"/bin/bash", "-c", "your_command", "with", "args"};
Process proc = new ProcessBuilder(args).start();
Answer from Rahul on Stack Overflow
🌐
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 - In this article, we will discuss how to run terminal commands from Java code. We can execute specific commands from the terminal to execute…
Discussions

Executing terminal commands from java - Stack Overflow
I know there are many threads about this, but none of them worked for me. Here is what I am trying to do: Javac and run a file from my java code. It works for Windows but I would like to make it a... More on stackoverflow.com
🌐 stackoverflow.com
How to run a simple Java program using terminal command? - Unix & Linux Stack Exchange
As you can see I'm trying to run a Java program but I need the command which I can write in terminal to run the code. More on unix.stackexchange.com
🌐 unix.stackexchange.com
April 2, 2017
Making a GUI to run commands on terminal?
You need something to create the ui. So maybe start with a look into Java fx 😊 More on reddit.com
🌐 r/learnjava
5
2
May 22, 2018
Can you create games that run in the terminal or command line that are like pong?
Oh, sure, just not easily at all. You'd need something like ncurses to do this for just any terminal. That's not a beginner's library at all. It's misleadingly difficult. The general idea is to have a single main loop that refreshes the game state (and thus what is drawn) at frequent intervals. You'd have to capture the keyboard inputs (which ncurses can do) to detect how to move the player's paddle. The physics of pong are very simple, so the main difficulty is figuring out how to draw things. More on reddit.com
🌐 r/learnprogramming
17
8
September 15, 2015
🌐
Xenovation
xenovation.com › home › blog › java › important java command line commands
Important Java command line commands | XENOVATION
March 30, 2019 - keytool - we can use this command to manage a keystore (database) of cryptographic keys · jarsigner - we can use this tool to sign and verify Java Archive (JAR) files
🌐
Quora
quora.com › How-do-I-use-terminal-commands-in-Java
How to use terminal commands in Java - Quora
chmod +x test.sh Now, write following lines of code in your Java file: [code]ProcessBuilder pb = new ProcessBuilder(
🌐
Codecademy
codecademy.com › article › java-for-programmers-java-and-the-command-line
Java and the Command Line | Codecademy
As a developer though, you need ... System.out.println(). The current version of Java we have here at Codecademy can be found by typing the command java --version into a terminal when you are doing a lesson....
🌐
freeCodeCamp
freecodecamp.org › news › how-to-execute-and-run-java-code
How to Execute and Run Java Code from the Terminal
March 10, 2022 - Or, if I want, I can also go there using my terminal. I need to use cd to indicate that I want to change directory. In this case, I can use cd "C:\Users\Md. Fahim Bin Amin\Documents". As my user name contains white spaces, I have used " " to enclose them. Then if I check all the files under that directory, then I will get the Main.java file as well. I placed the Main.java file under my D drive this time. So I went in that directory using the cd command.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › mac-cmd.html
Java and the Mac OS X Terminal
This document instructs you on how to use the Mac OS X Terminal with Java. You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. To verify that Apple's implementation of Java 2 Standard Edition (Java SE 6) is already installed: Run Software Update. Run Applications/Utilities/Java/Java Preferences and verify that the Java SE 6 - 64-bit entry is checked and first in the list; if not, drag to change the preferred order. You will type commands ...
Find elsewhere
🌐
Stack Abuse
stackabuse.com › executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - In this tutorial, we'll cover how to execute shell commands, bat and sh files in Java. We'll be covering examples for all exec() and ProcessBuilder approaches.
🌐
Medium
medium.com › @burakkocakeu › how-to-run-your-java-program-on-terminal-b4956e7102a8
How to Run your Java Program on Terminal | by Burak KOCAK | Medium
January 29, 2023 - You can execute the program by using the java command followed by the name of the class which contains the main method. The general syntax is java [class_name] The program will now execute and display any output in the terminal.
🌐
LaunchCode
education.launchcode.org › java-web-dev-curriculum › intro-and-setup › java-terminal › index.html
Java in the Terminal :: Java Web Development
October 1, 2021 - Whenever using the terminal in this course, use Git Bash as opposed to Windows Command Prompt. Recall from the walk-through on the previous page , Java needs to be compiled before executing. Java version 17 has the capability to compile single-file Java programs without explicitly running a command to compile.
🌐
CodingNomads
codingnomads.com › how-to-run-java-from-command-line
Executing Java applications from the CLI
A Java application can be run and compiled from the command line. Although running Java apps from the CLI during normal development is not entirely common, it is essential to know how it is done. Please follow the steps below to run your first Java app from the CLI. First, open your terminal and ...
🌐
Medium
medium.com › @AlexanderObregon › executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Learn how to compile and run Java programs from the command line using javac and java, manage classpaths, and understand how the JVM processes bytecode.
🌐
Dracula Servers
draculaservers.com › home › how to run java programs in linux terminal?
How to Run Java Programs in Linux Terminal? - Dracula Servers Tutorials
June 30, 2024 - Linux cannot compile or execute Java programs without the Java Development Kit. Therefore, the first step is to ensure the JDK has been installed on your machine. To verify this, open up a terminal (CTRL + ALT + T) and type in the following command inside it:
🌐
Trinity
cs.trinity.edu › ~bmassing › Misc › java-cmdline-howto
Java From the Command Line
So for example, this command runs the main method in class Hello: java Hello To pass command-line arguments to main in Hello, type them at the end of the above command separated by spaces.
🌐
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 - Quick guide to how to two ways of running a shell command in Java, both on Windows as well as on UNIX.
🌐
IONOS
ionos.com › digital guide › websites › web development › java commands
The most important Java commands in an overview
September 27, 2022 - Get an overview of the most important Java commands, which could help you program faster! This article lists the most used Java commands.
🌐
LabEx
labex.io › tutorials › java-how-to-compile-and-run-a-java-program-in-the-terminal-413960
How to compile and run a Java program in the terminal | LabEx
Additionally, the javac command supports various options and flags that can be used to customize the compilation process, such as setting the source and target Java versions, enabling or disabling certain compiler warnings, and more. You can explore these options by running javac -help in the terminal.