Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: IOException using runtime.exec
|
Posted: May 5, 2002 3:12 PM
|
|
Your problem may be how you are running your command. This program can be easily revised for Unix or Linux if needed. I no longer have a Unix or Linux machine to test it on. Check this out and see if anything in this source will help. I may have some other examples if I hunt around.
/* RuntimeDemo.java * @author: Charles Bell * @version: January 6, 2001 * Example use of Runtime. */
import java.io.*; import java.lang.*;
/** Executes the String arguments, displays the commands being executed, and displays * any results. */ public class RuntimeDemo{
/** Instantiates a class and runs the commands passed in the aguments. */ public static void main(String[] args){ RuntimeDemo demo = new RuntimeDemo(); demo.runCommand(args); }
/** Runs the commands in a process and displays the results. */ public void runCommand(String[] args){ int number = args.length; try{ String[] commands; String operatingsystem = System.getProperty("os.name"); if (operatingsystem.toLowerCase().indexOf("windows") > 0){ commands = new String[number + 2]; commands[0] = "command.com"; //for Windows 95, 98, ME. etc. if (operatingsystem.toLowerCase().indexOf("nt") > 0){ commands[0] = "cmd.exe"; //for Windows NT } commands[1] = "/c"; for (int i = 0; i < number;i++){ commands[i+2] = args[i]; } }else{ commands = new String[number]; for (int i = 0; i < number;i++){ commands[i] = args[i]; } }
System.out.print("Executing: "); for (int i = 0; i< commands.length;i++){ System.out.print(commands[i] + " "); }
Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(commands); // Because some native platforms only provide limited buffer size // for standard input and output streams, failure to promptly write // the input stream or read the output stream of the subprocess // may cause the subprocess to block, and even deadlock. CheckStream csin = new CheckStream(process.getInputStream()); CheckStream cserr = new CheckStream(process.getErrorStream()); csin.start(); cserr.start(); System.out.print("Waiting for command process to terminate."); int done = process.waitFor(); process.destroy(); System.out.println("... Done."); }catch(InterruptedException ie){ System.out.println("InterruptedException: " + ie.getMessage()); }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); } }
/** Inner class for checking the results if any of an InputStream. */ class CheckStream extends Thread{
BufferedReader br; String lineread = "";
/** Constructor needs an InputStream to form an anonymous * InputStreamReader which is used to create a BufferedReader * for reading the stream. */ CheckStream(InputStream is){ this.br = new BufferedReader(new InputStreamReader(is)); }
/** Reads the input stream and displays anything returned. */ public void run(){ try{ while ((lineread = br.readLine()) != null){ System.out.println(lineread); } }catch(IOException ioe){ System.out.println("IOException: " + ioe.getMessage()); } } } }
|
|