xref: /AOO41X/main/scripting/workben/installer/ExecCmd.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir package installer;
2*cdf0e10cSrcweir import java.util.*;
3*cdf0e10cSrcweir import java.io.*;
4*cdf0e10cSrcweir public class ExecCmd
5*cdf0e10cSrcweir {
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir     public boolean exec( String cmd, String[] env )
8*cdf0e10cSrcweir     {
9*cdf0e10cSrcweir        System.out.println("About to exectute " + cmd);
10*cdf0e10cSrcweir        final Process p;
11*cdf0e10cSrcweir        boolean result = false;
12*cdf0e10cSrcweir        try
13*cdf0e10cSrcweir        {
14*cdf0e10cSrcweir            Runtime rt = Runtime.getRuntime();
15*cdf0e10cSrcweir            p=rt.exec( cmd, env );
16*cdf0e10cSrcweir            new Thread(new Runnable() {
17*cdf0e10cSrcweir                public void run()
18*cdf0e10cSrcweir                {
19*cdf0e10cSrcweir                    BufferedReader br_in = null;
20*cdf0e10cSrcweir                    try
21*cdf0e10cSrcweir                    {
22*cdf0e10cSrcweir                        br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
23*cdf0e10cSrcweir                        String buff = null;
24*cdf0e10cSrcweir                        while ((buff = br_in.readLine()) != null)
25*cdf0e10cSrcweir                        {
26*cdf0e10cSrcweir                            System.out.println("Process out :" + buff);
27*cdf0e10cSrcweir                            /*try
28*cdf0e10cSrcweir                            {
29*cdf0e10cSrcweir                                Thread.sleep(100);
30*cdf0e10cSrcweir                            }
31*cdf0e10cSrcweir                            catch(Exception e) {}*/
32*cdf0e10cSrcweir                        }
33*cdf0e10cSrcweir                        System.out.println("finished reading out");
34*cdf0e10cSrcweir                     }
35*cdf0e10cSrcweir                     catch (IOException ioe)
36*cdf0e10cSrcweir                     {
37*cdf0e10cSrcweir                         System.out.println("Exception caught printing javac result");
38*cdf0e10cSrcweir                         ioe.printStackTrace();
39*cdf0e10cSrcweir                     }
40*cdf0e10cSrcweir                     finally
41*cdf0e10cSrcweir                     {
42*cdf0e10cSrcweir                        if ( br_in != null )
43*cdf0e10cSrcweir                        {
44*cdf0e10cSrcweir                            try
45*cdf0e10cSrcweir                            {
46*cdf0e10cSrcweir                                br_in.close();
47*cdf0e10cSrcweir                            }
48*cdf0e10cSrcweir                            catch( Exception e ) {} // nothing can be done
49*cdf0e10cSrcweir                        }
50*cdf0e10cSrcweir                     }
51*cdf0e10cSrcweir                } } ).start();
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir             new Thread(new Runnable() {
54*cdf0e10cSrcweir                 public void run() {
55*cdf0e10cSrcweir                 BufferedReader br_err = null;
56*cdf0e10cSrcweir                 try {
57*cdf0e10cSrcweir                     br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
58*cdf0e10cSrcweir                     String buff = null;
59*cdf0e10cSrcweir                     while ((buff = br_err.readLine()) != null) {
60*cdf0e10cSrcweir                     System.out.println("Process err :" + buff);
61*cdf0e10cSrcweir                     /*try {Thread.sleep(100); } catch(Exception e) {}*/
62*cdf0e10cSrcweir                  }
63*cdf0e10cSrcweir                  System.out.println("finished reading err");
64*cdf0e10cSrcweir                  } catch (IOException ioe) {
65*cdf0e10cSrcweir                     System.out.println("Exception caught printing javac result");
66*cdf0e10cSrcweir                     ioe.printStackTrace();
67*cdf0e10cSrcweir                  }
68*cdf0e10cSrcweir                  finally
69*cdf0e10cSrcweir                  {
70*cdf0e10cSrcweir                     if ( br_err != null )
71*cdf0e10cSrcweir                     {
72*cdf0e10cSrcweir                         try
73*cdf0e10cSrcweir                         {
74*cdf0e10cSrcweir                             br_err.close();
75*cdf0e10cSrcweir                         }
76*cdf0e10cSrcweir                         catch( Exception e ) {} // nothing can be done
77*cdf0e10cSrcweir                     }
78*cdf0e10cSrcweir                  }
79*cdf0e10cSrcweir             } }).start();
80*cdf0e10cSrcweir             int exitcode = p.waitFor();
81*cdf0e10cSrcweir             if ( exitcode != 0 )
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 System.out.println("cmd [" + cmd + "] failed" );
84*cdf0e10cSrcweir                 result= false;
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir             else
87*cdf0e10cSrcweir             {
88*cdf0e10cSrcweir                 System.out.println("cmd [" + cmd + "] completed successfully");
89*cdf0e10cSrcweir                 result= true;
90*cdf0e10cSrcweir             }
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         catch (Exception e) {
93*cdf0e10cSrcweir           System.out.println("Exception");
94*cdf0e10cSrcweir           e.printStackTrace();
95*cdf0e10cSrcweir        }
96*cdf0e10cSrcweir        System.out.println("command complete");
97*cdf0e10cSrcweir        return result;
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101