This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
THANKS!!!
Posted by Doug on April 17, 2001 at 5:43 PM
Thanks! As you stated, it worked flawlessly. > hi all, > try the following code works perfectly to run a batch file. and one more thing is you need not kill the dos prompt it automatically exits at the completion of your program execution . > I tested and it worked. All the best!! > Sarada Prapoorna.V > Goldstone Technologies LTD,Secunderabad,India. > > import java.io.*; > > public class RunBatch > { > public static void main(String args[]) { > try { > Process p = Runtime.getRuntime().exec("batchfile.bat"); > > BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); > BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); > > bw.write("1"); > bw.flush(); > String line; > while ((line = br.readLine()) != null) { > System.out.println(line); > } > br.close(); > bw.close(); > > System.out.println(p.exitValue()); > } > catch(Exception e) { > System.out.println(e); > } > } > }
Replies:
|