简体   繁体   English

Java-将命令执行到批处理文件中

[英]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. 我已经制作了一个Java脚本,该脚本使用runtime.exec()执行批处理文件,并且工作正常,但是当我获取输出流并使用write()函数时,它不会执行我放入其中的命令。

 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? 它显示批处理文件,但不运行命令,还有另一种方法可以在运行批处理文件的cmd中输入命令,以便显示它?

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. 它也应该工作刚刚CMD / C的build.bat,在这种情况下,如果需要的话,你可以阅读从子进程的输出在Java中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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