23

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.

Narinder Kumar
  • 461
  • 1
  • 5
  • 9

3 Answers3

30

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");
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • @ruakh Can you please tell me how I can do this wrapping with windows based PsExec.exe. As a example navigate to "C" drive and create a directory called "abc" ex: new ProcessBuilder("psexec", "-c", "cd c:\ ; mkdir abc");. Thanks a lot. – Channa Dec 09 '14 at 08:49
  • @Channa: I don't know about the general case, but I think your specific example could be written as `new ProcessBuilder("psexec", computer, "mkdir", "C:\\abc")`. – ruakh Dec 09 '14 at 21:12
7

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.

Rawhe Rawhe
  • 71
  • 1
  • 1
4

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758