如何使用Java运行批处理文件 [英] how to run a batch file using java
问题描述
我想使用Java程序运行批处理文件,当我双击.bat文件时,它要求我输入'D',然后在C驱动器中创建一些文件夹,下面是.bat的内容文件:
I want to run a batch file using a java program, when I double click the .bat file it asks me to enter 'D' and after that it creates some folders in C drive, below is the contents of the .bat file:
xcopy "data" "C:\data" /S
xcopy "rapid" "C:\rapid" /S
subst x: /D
subst x: C:\
我的Java代码如下:
My Java code is as below:
try {
//C:\Desktop\Speed\view_R36_WD_Release\RAPID\switchToLive.Bat
String cmds[] = {"C:\\Users\\608521747\\Desktop\\Speed\\view_R36_WD_Release\\RAPID\\switchToDev.bat"};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmds);
process.getOutputStream().close();
InputStream inputStream = process.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
BufferedReader bufferedrReader = new BufferedReader(inputstreamreader);
String strLine = "";
while ((strLine = bufferedrReader.readLine()) != null) {
System.out.println(strLine);
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
它不会给我任何错误,但是它既不要求我输入任何值也不它会创建任何文件夹。
its not giving me any errors but it neither ask me to enter any value nor it creates any folder.
我想知道我需要在Java代码中做什么,以便它会要求我输入'D',然后输入.bat。文件应该继续正常运行。
I want to know what do I need to do in java code so that it will ask me to enter the 'D' and then the .bat file should continue in a normal flow.
我们将不胜感激。
推荐答案
批处理文件不是可执行文件。因此,您将需要运行cmd.exe并将批处理文件作为参数传递。
Batch files are not executable files. So you will need to run cmd.exe and pass the batch file as parameter.
请参考这篇文章-它解决了相同的问题并提供了一个很好的解决方案-< a href = https://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application>如何从Java应用程序运行批处理文件?。
Please refer to this post - it addresses the same issue and provides a good solution - How do I run a batch file from my Java Application?.
这篇关于如何使用Java运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!