hello friends,

this is my attempt to execute an external cmd file through java and get the output printed to a text file while the program is running (not after the program has ended), since I need to do some real time manipulation for depending on the program output.

class RunnableThread implements Runnable {

	Thread runner;
	public RunnableThread() {
	public RunnableThread(String threadName) {
		runner = new Thread(this, threadName); // (1) Create a new thread.
		runner.start(); // (2) Start the thread.
	public void run() {

               File file=new File("C:/Documents and Settings/Administrator/Desktop/ant.txt");

		//Display info about this particular thread

                Runtime rt = Runtime.getRuntime();
                Process p1 = rt.exec(" rundll32 SHELL32.DLL,ShellExec_RunDLL " +"E:/HarvestCruise/build_current/source/_ant_compile.cmd");

                int p= p1.waitFor();
                }catch(InterruptedException f){}

               InputStream is=p1.getInputStream();
                int c;

                try {
			DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
                        System.out.println("I'm before the loop executes and the read value of c is "+is.read());

			while((c = is.read()) != -1) {
                            System.out.println("Im inisde the while loop and "+c);
		catch(IOException e) {
			System.err.println("Error Writing/Reading Streams.");

System.out.println("This is the exit value "+p1.exitValue());
                }catch(IllegalThreadStateException s){

                }catch(IOException k){}

and then I invoke this thread by,

public class Main {

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        RunnableThread thread3 = new RunnableThread("thread3");




but the problem is, isRead() always returns -1, hence always an empty ant.txt file is created where the shell output is supposed to be. any help is appreciated.
thanks a lot!

This article has been dead for over six months. Start a new discussion instead.