标签:
execruntime |
分类: J2SE |
import java.io.*;
import java.util.List;
import java.util.Vector;
public class Execute {
private
final static int OUTPUT_BUFFER_SIZE = 1024;
public
static void main(String[] args) throws Exception{
Vector
vector = new Vector(1,1);
vector.add("/bin/ls");
System.out.println(exec(vector));
}
/**
* Executes a method call.
*
* @param arguments a <tt>List</tt> of
<tt>String</tt> objects containing
the values
* of the arguments passed to the method.
* @return the <tt>TemplateModel</tt> produced by the
method, or null.
* @throws Exception
*/
public
static Object exec (List arguments) throws
Exception {
String aExecute;
StringBuffer
aOutputBuffer = new StringBuffer();
if( arguments.size() < 1 ) {
throw new Exception( "Need an argument to execute" );
}
aExecute = (String)(arguments.get(0));
try {
Process exec = Runtime.getRuntime().exec( aExecute );
// stdout from the process comes in here
InputStream execOut = exec.getInputStream();
InputStreamReader execReader = new InputStreamReader( execOut
);
char[] buffer = new char[ OUTPUT_BUFFER_SIZE ];
int bytes_read = execReader.read( buffer );
while( bytes_read > 0 ) {
aOutputBuffer.append( buffer, 0, bytes_read );
bytes_read = execReader.read( buffer );
}
}
catch( IOException ioe ) {
import java.util.List;
import java.util.Vector;
public class Execute
the values