Thursday, December 15, 2011

Java Tip of the Day: Using ProcessBuilder to Make System Calls

A few years ago I posted an article on how to execute OS level system calls using the Runtime class. When I wrote that article I was using a methodology which has been around since the 1.0 days of Java. In Java 5 Standard Edition, and subsequently improved in Java 6 and Java 7, is a class called ProcessBuilder. This class is a significant improvement over using the Runtime class. It provides complete flexibilty around the system call in terms of environment including directories, input and output streams, and execution.

Here is the NetBeans 7.1 Apache Maven project: process-builder-example.zip


When I execute it from NetBeans I get something like this:
[exec:exec] total 24 drwxr-xr-x 12 jyeary staff 408B Dec 15 08:19 .hg -rw-r--r-- 1 jyeary staff 52B Dec 15 08:08 .hgignore -rw-r--r-- 1 jyeary staff 1.7K Dec 15 07:59 nbactions.xml -rw-r--r-- 1 jyeary staff 2.0K Dec 15 08:19 pom.xml drwxr-xr-x 4 jyeary staff 136B Dec 14 23:31 src drwxr-xr-x 4 jyeary staff 136B Dec 15 08:35 target Exit Status : 0 

0 comments :

Popular Posts