Shell Class
Used to execute command line applications and return the corresponding
output streams (standard output and error output streams).
Constructors
public Shell( java.io.File executable, String[] parameters )
public Shell( javaxt.io.File executable, String[] parameters )
public Shell( String cmd )
public Shell( String[] cmdarray )
Methods
getEllapsedTime( ) returns long
Used to return the total time (milliseconds) it took to execute the
process.
getErrors( ) returns java.util.List
Used to retrieve the error output stream. Returns a List that can be
parsed while the executable is still running or after is has been run.
getOutput( ) returns java.util.List
Used to retrieve the standard output stream. Returns a List that can be
parsed while the executable is still running or after is has been run.
The difference lies in when the run method is invoked. The following is
an example of how to process the output stream while the app is running.
The getOutput() is called BEFORE the run() method is invoked.
File exe = new File("C:\\Program Files\\PostgreSQL\\8.4\\bin\\shp2pgsql.exe");
String[] options = new String[]{"-W", "UTF-8", "-s", "4326", "C:\country.shp", "t_country"};
javaxt.io.Shell cmd = new javaxt.io.Shell(exe, options);
java.util.List<String> output = cmd.getOutput();
cmd.run();
String line;
while (true){
synchronized (output) {
while (output.isEmpty()) {
try {
output.wait();
}
catch (InterruptedException e) {
}
}
line = output.remove(0);
}
if (line!=null){
System.out.println(line);
}
else{
break;
}
}
If you want to get the entire output all at once, just call the getOutput()
AFTER the run() method. Example:
javaxt.io.Shell cmd = new javaxt.io.Shell(exe, options);
cmd.run();
java.util.List output = cmd.getOutput();
for (int i=0; i
run( ) returns void
Used to execute the process specified in the constructor and populate
the output streams.
stop( ) returns void
Used to stop the current process. Note that this method does not stop
or kill process grandchildren. This is a limitation of Java, not this
class per se. See Sun bug 4770092 for more details.