Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: popen()-like function?

Tags:

java

popen

This is in the context of a local Processing program. I would like to run an external program to get some data. Is there a popen() or equivalent function I can use?

like image 495
Mark Harrison Avatar asked May 22 '10 10:05

Mark Harrison


2 Answers

Process process = Runtime.getRuntime().exec("your command");

Then you can read and write the data using the Process streams.

like image 113
Bozho Avatar answered Sep 25 '22 01:09

Bozho


JDK5 introduced ProcessBuilder for more control over the process generation.

Process process = new ProcessBuilder(command).start()

Be aware of the fact, that internally forkAndExec is invoked, and fork 'makes a copy of the entire parents address space', so that even a little command can lead to OutOfMemoryErrors, when the parent process has big amount of memory space acquired.

see here

like image 45
phi Avatar answered Sep 25 '22 01:09

phi