Advertisement
|
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:
i think this is the absolute solution to run a batch file through java
Posted by sasipriya on April 16, 2001 at 8:02 AM
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:
|