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 - Run terminal commands from Java In this article, we will discuss how to run terminal commands from Java code. We can execute specific commands from the terminal to execute processes in an operating …
🌐
Xenovation
xenovation.com › home › blog › java › important java command line commands
Important Java command line commands | XENOVATION
March 30, 2019 - javap - we can use this command to disassemble one or more class files · jmod - we can use this command to create JMOD files and list the content of existing JMOD files
Discussions

macos - How to run a Java Class in Terminal - Stack Overflow
Write a java program and save the file as filename.java. Now to compile use this command from the terminal: 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
🌐
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 ...
🌐
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(
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 19 › docs › specs › man › java.html
The java Command
December 12, 2022 - In other words, the item in the command line that would otherwise be the main class name. The --source version option, if present. If the class identifies an existing file that has a .java extension, or if the --source option is specified, then source-file mode is selected.
🌐
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.
🌐
Code With Arjun
codewitharjun.com › home › blogs › run java using command prompt / terminal
Run Java Using Command Prompt / Terminal - Code With Arjun
March 26, 2025 - Compile and Run Java Program using Command Prompt or Terminal, Notepad using Java commands: javac and java.
🌐
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.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to compile and run a java program using command prompt
How to Compile and Run a Java Program Using Command Prompt
September 28, 2025 - If you are unsure how to write a program in Java, check out our tutorial on how to program in Java. You can use any Java program to learn how to compile and run a program. ... Windows: Click the Windows Start menu and type CMD. Click the Command Prompt icon. Mac: In Finder, press the Go tab, select Applications, select Utilities, then select Terminal.
🌐
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.
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 9 › tools › tools-and-command-reference.htm
1 Tools and Commands Reference - Java
jconsole: You use the jconsole ... manage Java applications. jps: Experimental You use the jps command to list the instrumented JVMs on the target system. jstat: Experimental You use the jstat command to monitor JVM statistics. This command is experimental and unsupported. jstatd: Experimental ...
🌐
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.
🌐
LaunchCode
education.launchcode.org › java-web-development › chapters › introduction-and-setup › java-terminal.html
1.3. Java in the Terminal — Java Web Development documentation
We’ll discuss the syntax of this ... open a terminal window and navigate to the parent directory of your new file. Run: ... You should see your greeting printed! Recall from the walk-through on the previous page, Java needs to be compiled before executing. Java version 11 introduced the capability to compile single-file Java programs without explicitly running a command to ...