Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to Runtime and ProcessBuilder (Java)

I am interested if anyone has an idea on how to run unix commands without using runtime or ProcessBuilder in a java application

like image 488
tosi Avatar asked Aug 16 '12 13:08

tosi


People also ask

What does ProcessBuilder do in Java?

Class ProcessBuilder. This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes.

Is ProcessBuilder thread safe?

The ProcessBuilder Class Note that ProcessBuilder is not a synchronized class; hence, if it is not synchronized explicitly, it not thread safe to access the instance of this class through multiple threads.

How do I change the working directory in ProcessBuilder?

ProcessBuilder. directory(File directory) method sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory.


1 Answers

Well you can use DefaultExecutor from Apache Commons Exec library to execute commands but it internally uses java.lang.Runtime and java.lang.Process.

I would suggest you to use this library over Runtime because Apache Command Execution APIs are more sophisticated, and provide all and more features than Java Runtime. It also handles exit Values.

like image 114
Nandkumar Tekale Avatar answered Sep 25 '22 11:09

Nandkumar Tekale