It\'s been quite a while since I\'m looking for but I don\'t find the solution. I\'m trying to execute bash command on Linux within .jar file. For that, I tried many things, in
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.
I think the easiest way to do this would be to create a shell script (.sh extension) and then you can easily run that from within the Java program. There is a good answer on a previous question on how to run shell scripts within Java here.
To create a shell script you can use any text editor and create a file with the extension .sh and just enter the lines as you would in the bash terminal.