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.
System.out.println(runner.getName());
runner.start(); // (2) Start the thread.
}
public void run() {
try{
File file=new File("C:/Documents and Settings/Administrator/Desktop/ant.txt");
//Display info about this particular thread
System.out.println(Thread.currentThread());
Runtime rt = Runtime.getRuntime();
Process p1 = rt.exec(" rundll32 SHELL32.DLL,ShellExec_RunDLL " +"E:/HarvestCruise/build_current/source/_ant_compile.cmd");
try{
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);
out.writeByte(c);
}
is.close();
out.close();
}
catch(IOException e) {
System.err.println("Error Writing/Reading Streams.");
}
try{
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");
System.out.println(Thread.currentThread());
}
}
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!