The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2000

Advertisement

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:

Thanks Sasipriya

Posted by Amit on August 27, 2001 at 1:49 PM

Hi Sasipriya
Thanks for your code. I'm able to execute my batch file with the help of this code.

Amit

> 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:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us