Execute bash command within java program
To understand this, you first need to understand how you would run that command at a shell prompt.
$ sh -c "java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu"
Note where the double quotes are. The first argument is -c
. The second argument is the stuff inside the quotes; i.e. java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu
Now we translate that into Java:
Process p = new ProcessBuilder(
"/bin/sh",
"-c",
"java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu").start();
Having said that, the above doesn't actually achieve anything. Certainly, it doesn't open a fresh console window to display the console output etcetera. Unlike Windows "CMD.exe", UNIX / Linux shells do not provide console / terminal functionality. For that you need to use a "terminal" application.
For example, if you are using GNOME
Process p = new ProcessBuilder(
"gnome-terminal",
"-e",
"java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu").start();
will (probably) do what you are trying to do.
execute bash script from java
Recently I used the below approach to execute a bash script.
Process exec = getRuntime().exec("/home/user/test/test.sh");
java.util.Scanner s = new java.util.Scanner(exec.getInputStream()).useDelimiter("\\A");
System.out.println(s.next());
Whenever I tried getRuntime().exec("./home/user/test/test");
I got the exact error you were getting. java.io.IOException: Cannot run program "./home/user/test/test": error=2, No such file or directory
.
To execute any command from any directory, please follow the below approach.
String []command ={"/bin/bash","-c", "ls"};
Process exec = getRuntime().exec(command,null,new
File("/home/user/test"));
java.util.Scanner s = new java.util.Scanner(exec.getInputStream()).useDelimiter("\\A");
System.out.println(s.next());
Hope this is some way helpful.
How to execute Shell Commands with Java and print the output directly while executing the command
Consider the following code.
ProcessBuilder pb = new ProcessBuilder("ping", "localhost");
pb.inheritIO();
try {
Process p = pb.start();
int exitStatus = p.waitFor();
System.out.println(exitStatus);
}
catch (InterruptedException | IOException x) {
x.printStackTrace();
}
I believe the above does what you want and I would say that the code is a lot simpler.
Refer to the javadoc for class java.lang.ProcessBuilder
.
How to invoke a Linux shell command from Java
exec does not execute a command in your shell
try
Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});
instead.
EDIT::
I don't have csh on my system so I used bash instead. The following worked for me
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});
Execute shell commands by ProcessBuilder In java but seems nothing work
Java isn't bash, or shell. You don't really ever want to use exec
, its rules are somewhat bizarre. It tries very haphazardly to split on spaces.
Instead, use ProcessBuilder
, and specify a list of arguments, not the single string variant.
That whole pg_dump thing is a single argument and should not contain quotes - those quotes are 'eaten' by bash and tell bash to treat it all as a single argument; java isn't bash and just blindly passes them to the kubectl tool which doesn't know what it means.
All the other args (including --
) are their own thing.
So..
List.of("/bin/kubectl", "exec", "-it", "pg-container", "-n", "data-prod", "--", "/bin/bash", "-c", String.format("pg_dump -U %s -d %s -n %s %s -f %s.sql", dbinfo.dbuser, dbinfo.dbname, dbinfo.schemaname, query_tables, dbinfo.output));
And pass that to the constructor of ProcessBuilder
.
Related Topics
How to Encode a Wav to a Mp3 on a Android Device
Read_External_Storage Permission for Android
Select Random Document from Firestore
Inspecting Java Threads in Linux Using Top
How to Find a User's Home Directory on Linux or Unix
Amazon Ec2 Ftp Write Permission
What's the Location of the Javafx Runtime Jar File, Jfxrt.Jar, on Linux
Tool for Creating a Java Daemon Service on Linux
Java Is Installed, in Listing, But Execution Produces "./Java: No Such File or Directory"
Verify If a Process Is Running Using Its Pid in Java
Low Java Single Process Thread Limit in Red Hat Linux
How to Get the Ip of the Computer on Linux Through Java
How to Create a Linux Cluster for Running Physics Simulations in Java
Can the Java Cookbook Be Used to Install a Local Copy of Oracle Java