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 Top answer 1 of 6
59
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();
2 of 6
23
I vote for Karthik T's answer. you don't need to open a terminal to run commands.
For example,
// file: RunShellCommandFromJava.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RunShellCommandFromJava {
public static void main(String[] args) {
String command = "ping -c 3 www.google.com";
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
}
}
The output:
$ javac RunShellCommandFromJava.java
$ java RunShellCommandFromJava
PING http://google.com (123.125.81.12): 56 data bytes
64 bytes from 123.125.81.12: icmp_seq=0 ttl=59 time=108.771 ms
64 bytes from 123.125.81.12: icmp_seq=1 ttl=59 time=119.601 ms
64 bytes from 123.125.81.12: icmp_seq=2 ttl=59 time=11.004 ms
--- http://google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 11.004/79.792/119.601/48.841 ms
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
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
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
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
Videos
04:54
Run Java using Terminal on MacOS | Compile and Run java using Command ...
02:21
Run java program using command prompt/terminal | how to run java ...
06:21
Command Line Arguments in Java - YouTube
08:55
How to Run a Java File from Terminal or Command Prompt with ...
06:27
How to Compile and Run Java Program from Command Prompt - YouTube
03:32
Run Java in Command Prompt / Terminal | Compile and Run Java using ...
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 ...
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.
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 ...
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.
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.