24

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.

3 Answers 3

31

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"); 
Sign up to request clarification or add additional context in comments.

2 Comments

@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: 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").
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.

2 Comments

/c is for auto close
This just build the single string containing 2 commands. This is an equivalent of the @ruakh example, but for Windows. In *NIX example the ; is used instead of &
4

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

1 Comment

Can you share an example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.