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:

Try this

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

Hi
Try the following code

***********Code Starts******************


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);
}
}
}

********** Code Ends ****************


> Hi,
> I was trying to run a batch file from my program, so I did the following:
> public static void main (String []args) {
> /*
> Threadtest t = new Threadtest();
> t.start();*/
> testrun();
> }

> public static void testrun () {
> BufferedReader br = null;
> BufferedReader br2 = null;
> Process p = null;
> int status = -1;
> try {
> String osName = System.getProperty("os.name");
> if (osName.compareTo("Windows NT") == 0) {
> //String []test = "cmd.exe", "/c", "runtest.bat");
> p = Runtime.getRuntime().exec("cmd.exe /c runtest.bat ");
> }
> br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
> String s;
> while ((s = br.readLine())!= null) {
> System.out.println(s);
> }
> br2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
> while ((s = br2.readLine())!= null) {
> System.out.println(s);
> }
> br.close();
> br2.close();
> }catch (IOException ioex) {ioex.printStackTrace();}
> //return 0;
> }
> Now, this starts the dos prompt, and then the program waits on it.
> It only continues after I kill the dos prompt. Is there any way that I can start the process and then forget about it?
> I dont want to have any control after spawning off the process.
> Could anyone help me with this problem?
> Thanks
> Sasipriya






Replies:

Sponsored Links



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