Run docker image build from java process using getRuntime.exec() hangs

Trying to run “docker image build” command from Java programmatically. All of my docker commands using getRuntime.exec() have been working except this one. I know that the dockerfile path is correct, because if it isn’t the exec() does not hang and just prints a file not found error.

String[] args= new String[] {"docker", "build", "-f", resource, "-t", "mydocker:1.0", "."};
Process p = Runtime.getRuntime().exec(args);

BufferedReader stdInput = new BufferedReader(new
    InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
    InputStreamReader(p.getErrorStream()));

// read the output from the command
String s;
while ((s = stdInput.readLine()) != null) {
    log.debug("**Docker** => " + s);
    System.out.println(s);
}

// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
    log.debug("**Docker [Error]** => " + s);
    System.out.println(s);
}

stdInput.close();
stdError.close();

Source: StackOverflow