KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Execute


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.io.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * Class for running programs and executing commands.
17  * The methods in this class catch and return output from these commands (both info and error messages).
18  *
19  * @application VWMS
20  * @author Daniel Ockeloen
21  * @author Pierre van Rooden (javadocs)
22  * @version $Id: Execute.java,v 1.1 2005/01/30 16:46:40 nico Exp $
23  */

24 public class Execute {
25     // logger
26
private static Logger log = Logging.getLoggerInstance(Execute.class.getName());
27
28     /**
29      * Executes a command or program.
30      * The output of the program is returned as a string.
31      * @param command An array of strings, in which teh first argument is the command to execute,
32      * and the rest its parameters
33      * @return the command output
34      */

35     public String JavaDoc execute (String JavaDoc command[]) {
36         Process JavaDoc p=null;
37         String JavaDoc s="",tmp="";
38
39         BufferedReader dip= null;
40         BufferedReader dep= null;
41
42         try {
43             p = (Runtime.getRuntime()).exec(command,null);
44             p.waitFor();
45         } catch (Exception JavaDoc e) {
46             s+=e.toString();
47             return s;
48         }
49
50         dip = new BufferedReader( new InputStreamReader(p.getInputStream()));
51         dep = new BufferedReader( new InputStreamReader(p.getErrorStream()));
52
53         try {
54             while ((tmp = dip.readLine()) != null) {
55                    s+=tmp+"\n";
56             }
57             while ((tmp = dep.readLine()) != null) {
58                 s+=tmp+"\n";
59             }
60         } catch (Exception JavaDoc e) {
61             return s;
62         }
63         return s;
64     }
65
66     /**
67      * Executes a command or program.
68      * The output of the program is returned as a string.
69      * @param command the command to execute
70      * @return the command output
71      */

72     public String JavaDoc execute (String JavaDoc command) {
73         Process JavaDoc p=null;
74         String JavaDoc s="",tmp="";
75
76         BufferedReader dip= null;
77         BufferedReader dep= null;
78
79         try {
80             p = (Runtime.getRuntime()).exec(command,null);
81             p.waitFor();
82         } catch (Exception JavaDoc e) {
83             s+=e.toString();
84             return s;
85         }
86
87         dip = new BufferedReader( new InputStreamReader(p.getInputStream()));
88         dep = new BufferedReader( new InputStreamReader(p.getErrorStream()));
89
90         try {
91             while ((tmp = dip.readLine()) != null) {
92                    s+=tmp+"\n";
93             }
94             while ((tmp = dep.readLine()) != null) {
95                 s+=tmp+"\n";
96             }
97         } catch (Exception JavaDoc e) {
98             return s;
99         }
100         return s;
101     }
102
103     /**
104      * Outputs debug code.
105      * Not used.
106      */

107     private void writeLog( String JavaDoc msg ) {
108         log.error(msg);
109     }
110
111     /**
112      * Entry for direct invocation from the commandline.
113      * Usage:<br />
114      * java Execute [command]<br/>
115      * Does not take parameters.
116      * @deprecated Only for testing. I mean, why bother?
117      */

118     public static void main(String JavaDoc args[]) {
119         Execute execute = new Execute();
120         log.info(execute.execute(args[0]));
121     }
122 }
123
Popular Tags