0

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.

6
  • 1
    Can you check what is the content of process.getErrorStream(), process.getInputStream() and process.getOutputStream()? Commented Jun 29, 2015 at 11:47
  • ... and what do you mean by "runs properly"? what is it supposed to be doing? Commented Jun 29, 2015 at 11:48
  • Check this if it helps: stackoverflow.com/questions/27970455/… Commented Jun 29, 2015 at 11:50
  • @HovercraftFullOfEels I meant... The batch file makes the report file when I execute in cmd. Commented Jun 29, 2015 at 11:53
  • "c:/Users/..."=>"c:\\Users\\..." Commented Jun 29, 2015 at 11:56

1 Answer 1

1

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");
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.