×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Marcelo Aleks
Added: May 23, 2015 4:40 PM
Views: 9
Tags: no tags
  1. public class ProcessBuildDemo {
  2.     public static void main(String [] args) throws IOException {
  3.        
  4.         String[] command = {"CMD", "/C", "dir"};
  5.         ProcessBuilder probuilder = new ProcessBuilder( command );
  6.         //You can set up your work directory
  7.         probuilder.directory(new File("c:\\xyzwsdemo"));
  8.        
  9.         Process process = probuilder.start();
  10.        
  11.         //Read out dir output
  12.         InputStream is = process.getInputStream();
  13.         InputStreamReader isr = new InputStreamReader(is);
  14.         BufferedReader br = new BufferedReader(isr);
  15.         String line;
  16.         System.out.printf("Output of running %s is:\n",
  17.                 Arrays.toString(command));
  18.         while ((line = br.readLine()) != null) {
  19.             System.out.println(line);
  20.         }
  21.        
  22.         //Wait to get exit value
  23.         try {
  24.             int exitValue = process.waitFor();
  25.             System.out.println("\n\nExit Value is " + exitValue);
  26.         } catch (InterruptedException e) {
  27.             // TODO Auto-generated catch block
  28.             e.printStackTrace();
  29.         }
  30.     }
  31. }