简体   繁体   中英

java ProcessBuilder doesn't work

I'm trying to execute batch file in 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.

The only output I can see is just exit Value is 1 .

When I execute this command line in cmd, it runs properly but not in my java source.

What should I do?

Thanks for your help in advance.

Try adding the /C option to carry out the batch command

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");
...

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