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");
Answer from ruakh on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 21270125 › processbuilder-with-multiple-commands
java - ProcessBuilder with multiple commands - Stack Overflow
private static final String[][] commands = { {"rm", "-f", "/tmp/airpi_pipe"}, {"mkfifo", "/tmp/airpi_pipe"}, {"tail", "-f", "/dev/null", ">", "/tmp/airpi_pipe"} }; public static void main(String[] args) throws IOException { for (String[] str : commands) { ProcessBuilder pb = new ProcessBuilder(str); pb.redirectErrorStream(true); Process process = pb.start(); InputStream is = process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } System.err.println("next one"); } }
🌐
Mkyong
mkyong.com › home › java › java processbuilder examples
Java ProcessBuilder examples - Mkyong.com
January 19, 2019 - ProcessBuilder processBuilder = new ProcessBuilder(); // -- Linux -- // Run a shell command processBuilder.command("bash", "-c", "ls /home/mkyong/"); // Run a shell script processBuilder.command("path/to/hello.sh"); // -- Windows -- // Run a command processBuilder.command("cmd.exe", "/c", "dir C:\\Users\\mkyong"); // Run a bat file processBuilder.command("C:\\Users\\mkyong\\hello.bat"); Process process = processBuilder.start(); 1.1 Run an external ping command to ping a website 3 times, and display the output. ... package com.mkyong.process; import java.io.BufferedReader; import java.io.IOExce
🌐
Stack Overflow
stackoverflow.com › questions › 32250225 › java-start-multiple-commands-strictly-sequentially
processbuilder - Java start multiple commands strictly sequentially - Stack Overflow
works fine, however, I want to call Runtime.exec/ProcessBuilder.start() only once and return immediately. So basically it should work as if I called a batch file containing those two commands from a command line tool (what I can't do, because those two commands are generated dynamically).
Top answer
1 of 1
1

One problem is that your while loops

while ((buff = reader.readLine()) != null) { ... }

only terminate when the reader has reached the end of the input stream.

The end of the input stream is only reached when the subprocess terminates.


The second problem is that you seem to be using NodeJS as sample command executor.

  • If NodeJS is started from a console, you can enter JavaScript statements and they are executed one by one.
  • You are however starting NodeJS not from a console, but from some other application. In this case, NodeJS wants to read a complete script from stdin and executes the complete script at once
  • You could start NodeJS with the -i parameter (force interactive mode), at the expense of some additional output.

To achieve this, you would create the ProcessBuilder with

ProcessBuilder pb = new ProcessBuilder("node", "-i");

Communicating with a subprocess in this way only works when you know how many lines to read from the reader before sending the next command.

Knowing how many lines to read can mean:

  • knowing how many lines of output a command produces (login: 1 line of output, logout: no output)
  • knowing that a command produces a distinct last line (for example an empty line or a line with only "END" in it)
  • that a command produces a line count as the first result
  • executing another command first that returns the result line count of the subsequent command

The list certs command could either:

  • produce

      cert1 abc
      cert2 def
      END
    
  • produce (where the last line would be empty instead of containing a dot)

      cert1 abc
      cert2 def
      .
    
  • produce

      2 certs
      cert1 abc
      cert2 def
    
  • or you could execute a count certs command before executing list certs

🌐
Stack Overflow
stackoverflow.com › questions › 73303103 › java-processbuilder-execute-multiple-commands-in-sequence-on-same-java-program
process - Java ProcessBuilder execute multiple commands in sequence on same Java program - Stack Overflow
August 10, 2022 - import java.util.Scanner; public class test { public static void main(String args[]){ Scanner sc = new Scanner(System.in); sc.nextLine(); sc.nextLine(); System.out.println("you have done it"); } } public class test2 { public static void main(String args[]){ Process process; ProcessBuilder pb = new ProcessBuilder("java -jar Test.jar"); try { process = pb.start(); process.waitFor(); ProcessBuilder pb1 = new ProcessBuilder("no"); Process process1 = pb1.start(); process1.waitFor(); ProcessBuilder pb2 = new ProcessBuilder("no"); Process process2; process2 = pb2.start(); process2.waitFor(); System.out.println(process2.getInputStream()); }catch(Exception e){ } } }
🌐
Experts Exchange
experts-exchange.com › questions › 26637133 › How-do-I-run-two-consecutive-DOS-commands-through-a-ProcessBuilder-object.html
Solved: How do I run two consecutive DOS commands through a ProcessBuilder object? | Experts Exchange
November 24, 2010 - ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe /c dir & exit"); processBuilder.start(); Select allOpen in new window Though the exit is superfluous here. ... You may also want to explore your options with that "&" symbol - One ampersand means do this AND this Two together (&&) mean do thing two only if thing one succeeded (nonzero exit code) Two Pipes (||) mean do thing two only if thing one failed you can also use parenthesis to logically group commands.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 23707613 › multiple-command-in-java-processbuilder
Multiple command in java processbuilder - Stack Overflow
May 17, 2014 - Here is an instructional example to do the above in Java. This example simply writes the final output to a file but you can use a stream reader as in your existing code. Tweak, add error checking, exception handing and optimize as necessary. ( and don't write to root dir in a production application, use user temp). final ProcessBuilder processBuilder1 = new ProcessBuilder("cmd.exe", "/c", "dir", "c:\\"); final ProcessBuilder processBuilder2 = new ProcessBuilder("cmd.exe", "/c", "find", "\"DIR\""); processBuilder1.redirectOutput(Redirect.to(new File("c:\\tmp1.tmp"))); final Process process1 = processBuilder1.start(); process1.waitFor(); processBuilder2.redirectInput(Redirect.from(new File("c:\\tmp1.tmp"))); processBuilder2.redirectOutput(Redirect.to(new File("c:\\tmp2.tmp"))); final Process process2 = processBuilder2.start(); process2.waitFor();
🌐
Stack Overflow
stackoverflow.com › questions › 23734703 › multiple-command-in-process-builder
java - multiple command in process builder - Stack Overflow
You need to brush up your understanding of the process model, and read the ProcessBuilder documentation in light of that. What your code does is, create a process to run the command "nusmv -int D:/files/bitshift.smv", then create a new process to (try to) run the command "go", and so on.
🌐
Stack Overflow
stackoverflow.com › questions › 24230003 › multiple-execution-of-a-processbuilder-in-a-java-program
Multiple execution of a ProcessBuilder in a Java program - Stack Overflow
while(rs1.next()) { instance_id = rs1.getString(1); startdate = rs1.getString(2); starttime = rs1.getString(3); endtime = rs1.getString(4); enddate = rs1.getString(5); if(presentdate.equals(startdate) || presentdate.equals(enddate)) { if(presenttime.equals(starttime)) { String[] s1 = new String[]{"cmd", "/c","ec2-start-instances",instance_id,">>","D:\\logfile.log"}; ProcessBuilder builder1 = new ProcessBuilder(s1); Process p1 = builder1.start(); } else if(presenttime.equals(endtime)) { String[] s1 = new String[]{"cmd", "/c","ec2-stop-instances",instance_id,">>","D:\\logfile.log"}; ProcessBuilder builder1 = new ProcessBuilder(s1); Process p1 = builder1.start(); } } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › ProcessBuilder.html
ProcessBuilder (Java Platform SE 8 )
April 21, 2026 - The start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes. Each process builder manages these process attributes: a command, a list of strings which signifies the external program file to be invoked and its arguments, if any.
🌐
Aviator Dao
java2novice.com › главная страница › welcome to aviator dao from the creators of the java2novice
From Java Programming to Aviator Game: Explore Aviator DAO
July 17, 2024 - Discover the evolution of our journey from Java programming tutorials to the exciting world of the Aviator Game. At Aviator DAO, we provide in-depth guides, strategies, and resources for mastering Aviator.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.