Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ProcessBuilder to start execute multiple commands sequentially in Linux

Tags:

I would like to execute 2 or more commands sequentially through my Java Application using ProcessBuilder class. I Have tried multiple options as suggested in other responses/forums but no luck.

Here are the things I have tried:

    ProcessBuilder processBuilder = new ProcessBuilder("ls", ";", "pwd");

Gives me following error :

Errors : ls: ;: No such file or directory Errors : ls: pwd: No such file or directory

    ProcessBuilder processBuilder = new ProcessBuilder("ls", "&&", "pwd");

Gives me similar error:

Errors : ls: &&: No such file or directory Errors : ls: pwd: No such file or directory

    List<String> command = new ArrayList<String>();
    command.add("ls");
    command.add(";");
    command.add("pwd");
    ProcessBuilder processBuilder = new ProcessBuilder(command);

Gives me following error:

Errors : ls: ;: No such file or directory Errors : ls: pwd: No such file or directory

My OS is Linux/Mac-OSX.

like image 311
Narinder Kumar Avatar asked Sep 04 '12 17:09

Narinder Kumar


People also ask

Can be used to run multiple commands one after the other?

The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds.

How do you run multiple commands?

Running Multiple Commands as a Single Job We can start multiple commands as a single job through three steps: Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.


3 Answers

Your approaches are equivalent to calling ls with the specified arguments. In Bash notation, what you're running is:

ls ';' pwd
ls '&&' pwd

If you want ls and pwd to be run as separate commands, you can use Bash (or another shell language) to wrap them into a single command:

bash -c 'ls ; pwd'

which you can call this way:

ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ls ; pwd");
like image 99
ruakh Avatar answered Oct 17 '22 07:10

ruakh


I'm using ProcessBuilder to compile java program like this and it works for me:

ProcessBuilder b = new ProcessBuilder("cmd.exe","/c","cd " + dir,
            " & javac " + mapClassName + ".java -cp " + pathToProjectClasses);
  • cmd.exe : it's start the command prompt.
  • \c : not sure what its doing but its important, you can see this link for more information (\? cmd commands)
  • cd + dir : is the first command and its change the directory to a certain path which is dir.
  • & : its mean start the second command after you finish the first one
  • javac : this word and the rest of the string is the second command
  • -cp : path to external class used by the class you want to compile.

So I have 2 commands, first one is cd command and second one is javac command and i execute them sequentially using &.

Sorry for my bad writing skills, if I haven't explained my code well please ask me about anything you want to know.

like image 44
Rawhe Rawhe Avatar answered Oct 17 '22 07:10

Rawhe Rawhe


You could get the Process from ProcessBuilder.start() from the first command, wait using waitFor() and then launch the second one.

like image 21
Denys Séguret Avatar answered Oct 17 '22 09:10

Denys Séguret