简体   繁体   中英

Java - Executing commands into a batch file

I have made a java script which uses the runtime.exec() to execute a batch file, and that works fine but when i get the output stream and use the write() function it does not execute the command i put into it.

 Runtime runtime = Runtime.getRuntime(); Process p; p = runtime.exec("cmd /c start batchfile.bat"); out = p.getOutputStream(); out.write("command".getBytes()); 

It displays the batch file but does not run the command, is there another way of entering a command into the cmd running the batch file so it displays it?

With the start command, a separate command window will be opened, and any output from the batch file will be displayed there. It should also work as just cmd /c build.bat, in which case you can read the output from the subprocess in Java if desired.

You're writing into an output stream. I think you mean to write to an input stream.

Try this:

Runtime runtime = Runtime.getRuntime();
Process p;
p = runtime.exec("cmd /c start batchfile.bat");     
in = p.getInputStream();
in.write("command".getBytes());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM