View Single Post
  #2 (permalink)  
Old 06-12-2007, 01:26 PM
levent levent is offline
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Hello Gary,

If you have Java 5 and above you should use ProcessBuilder instead of Runtime.exec(). Here is an example program for your purpose:

Code:
import java.io.*; import java.util.*; public class DoProcessBuilder { public static void main(String args[]) throws IOException { if (args.length <= 0) { System.err.println("Need command to run"); System.exit(-1); } Process process = new ProcessBuilder(args).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf("Output of running %s is:", Arrays.toString(args)); while ((line = br.readLine()) != null) { System.out.println(line); } } }
You can run it with "java DoProcessBuilder whoami".

Let us know if you have any other problems.
Reply With Quote