[英]How to run a .exe file through the command prompt in a Java program?
I've read other questions about running .exe files through command prompt but the answers given did not work for me--I tried several. 我已经阅读了有关通过命令提示符运行.exe文件的其他问题,但给出的答案对我不起作用-我尝试了几次。 I am generating input files for this .exe and I need to run them and get the output.
我正在为此.exe生成输入文件,我需要运行它们并获取输出。
The command is this: C:\\exeDir\\myExe.exe -b C:\\inDir\\in.txt C:\\outDir\\out.txt 命令是这样的:C:\\ exeDir \\ myExe.exe -b C:\\ inDir \\ in.txt C:\\ outDir \\ out.txt
I have tried: 我努力了:
Process process = new ProcessBuilder("C:\\exeDir\\myExe.exe",
"-b C:\\inDir\\in.txt",
"C:\\outDir\\out.txt").start();
And 和
String[] cmd = { "C:\\exeDir\\myExe.exe",
"-b C:\\inDir\\in.txt",
"C:\\outDir\\out.txt" };
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
As well as a few other variations. 以及其他一些变化。 None have worked.
没有一个工作。 What is the proper way for me to handle this?
我处理此问题的正确方法是什么? Thanks.
谢谢。
You're on the right track, 您走在正确的轨道上,
try {
Process process = new ProcessBuilder(
"C:\\exeDir\\myExe.exe",
"-b", "C:\\inDir\\in.txt",
"C:\\outDir\\out.txt").start();
// to get the result...
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
// Process finished.
} catch (Exception e) {
// print any errors.
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.