[英]java ProcessBuilder doesn't work
I'm trying to execute batch file in java. 我正在尝试在Java中执行批处理文件。
My source is below: 我的来源如下:
List<String> comm = new ArrayList<String>();
comm.add("cmd");
comm.add("c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat");
comm.add("-d");
comm.add("C:\\Users\\cointreau\\workspace\\counter\\src\\Counter.java");
comm.add("-f");
comm.add("xml");
comm.add("-R");
comm.add("java-codesize");
comm.add("-r");
comm.add("C:\\Users\\cointreau\\workspace\\counter\\report.xml");
ProcessBuilder probuilder = new ProcessBuilder( comm );
Process process = probuilder.start();
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the original command line is this, 原来的命令行是这个,
c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat -d C:\\Users\\cointreau\\workspace\\counter\\src\\Counter.java -f xml -R java-codesize -r C:\\Users\\cointreau\\workspace\\counter\\report.xml`
pmd.bat
is the batch file what i want to execute and the remainders are just parameters for the bat file. pmd.bat
是我要执行的批处理文件,其余的只是bat文件的参数。
The only output I can see is just exit Value is 1
. 我可以看到的唯一输出就是
exit Value is 1
。
When I execute this command line in cmd, it runs properly but not in my java source. 当我在cmd中执行此命令行时,它可以正常运行,但不能在我的Java源代码中运行。
What should I do? 我该怎么办?
Thanks for your help in advance. 感谢您的帮助。
Try adding the /C
option to carry out the batch command 尝试添加
/C
选项以执行批处理命令
comm.add("cmd");
comm.add("/c");
comm.add("c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat");
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.