1

I am facing below error:

java.io.IOException: Cannot run program "C:\abc\man\b\manu.bat C:\Users\12x\test\testFiles\abc.properties" (in directory "C:\Users\12x\test\testFiles\abc.properties"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048).

Please find the code i am using:

public class TestProcess {


public TestProcess(Path workPath, Path exe, Path logbackConfig, 
                     Path propertyfile) throws IOException {

    String exeSuffix = "";
    if (OS.indexOf("win") >= 0) {
      exeSuffix = ".bat";
    }
    builder = new ProcessBuilder()
            .directory(workPath.toFile())

            .command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix+ " " + propertyfile)
            .redirectOutput(Redirect.INHERIT)
            .redirectError(Redirect.INHERIT);

My aim is to run a bat file(which is present in C:\abc\man\b folder) followed by abc.properties(which is in another folder C:\Users\12x\test\testFiles).

In code above, workPath has the value

C:\abc\man\b

and propertyfile has

C:\Users\12x\test\testFiles
user207421
  • 305,947
  • 44
  • 307
  • 483
sandeep goyal
  • 11
  • 1
  • 6

2 Answers2

0

You can't exec() a .bat file directly in Windows. You have to interpose cmd /c.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

You do not use the correct syntax: you can not concatenate the program and its arguments in a string, since ProcessBuilder is not a parser.

Instead, build a String named propertyfile_path_string corresponding to the properties file, and replace your line .command(...) by this one:

.command(workPath.resolve(exe).toAbsolutePath().toString() + exeSuffix, propertyfile_path_string)
Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24