Java Concurrency(pseudo -parallelism): Part I

  • ProcessBuilder class is introduced in JDK 5.0, is used to create processes.
  • This class is not synchronised
  • Package :- java.lang.ProcessBuilder
// Java code illustrating command() method
import java.lang.*;
class ProcessBuilderDemo{
public static void main(String[] arg) throws IOException{
// creating list of process
List<String> list = new ArrayList<String>();
// create the process
ProcessBuilder build = new ProcessBuilder(list);
// checking the command i list
System.out.println("command: " + build.command());
O/p - command: [notepad.exe]
Process process = new ProcessBuilder("java", "- version").start(); 
ProcessBuilder processBuilder = new ProcessBuilder();
Map<String, String> environment = processBuilder.environment();
environment.forEach((key, value) -> System.out.println(key + value));
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "echo Hello %name%");
Map<String, String> environment = processBuilder.environment();
environment.put("name", "Shivam");
Process p = processBuilder.start();
String line;
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
O/P - Shivam
ProcessBuilder processBuilder = new ProcessBuilder("java","version");
File log = folder.newFile("java-version.log");
Process process = processBuilder.start();
String[] command = {"/teste/teste_back/", argument1, argument};
ProcessBuilder p = new ProcessBuilder(command);
try {
// create a process builder to send a command and a argument
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));

String line;"Output of running " + command + " is: ");
System.out.println("Output of running " + command + " is: ");
while ((line = br.readLine()) != null) {;
  • Before JDK 5.0, Runtime.exec() is used to create the process.
Runtime r = Runtime.getRuntime();
Process p = r.exec("firefox");
p.waitFor(10, TimeUnit.SECONDS);
String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);




Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Myth: In Scrum, we spend too much time in meetings

The best way to kick ass as a junior developer

The 5 Best Mobile App Developers on Fiverr.

Product development in a vacuum

How an Odoo developer helps your business to grow

Hire odoo developer

Auto-Scaling Build Agents for GoCD

Export SQL Server Tables From database to Excel File in SSIS

Postgres can do THAT?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kumar Shivam

Kumar Shivam

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

More from Medium

Debugging RAM: Java Garbage Collection — Java Heap Deep Dive, Part 1

Inside Java Virtual Machine

Unit Testing Apache Camel

The optimal approach to find the nth Fibonacci number using recursion in Java