I need to run some docker commands from a java application. So far I've searched and tried both runtime exec and processBuilder approaches but it doesn't work. My last try was with processBuilder trying to execute a bash script containing a docker pull command.
Nothing was printed to the eclipse IDE console, although when i modified the .sh file to do echo 'i am responding' it was printed to the console. To debug the code i modified the .sh script to redirect the stdout and stderr of the docker pull to a file. After opening the resulting file i got: line 11: docker: command not found whereas when i run it in my terminal the docker command works fine. Therefore i am assuming the problem is lies in the java code itself. Where am i going wrong?. Any help would be much appreciated.
Note: I also heard of and tried Spotify docker-client library but I guess my knowledge of Docker is not enough at the moment to set it up. I am using MacOS Big Sur. Here is the java code:
public static void main(String[] args) throws IOException {
Common cm = new Common();
String userDir = cm.getuserDirectory();
String sp = cm.getfileSeparator();
String filePath = userDir + sp + "src" + sp + "data" + sp + "pull-and-run.sh";
String[] cmd = {"sh", filePath};
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
processBuilder.directory(new File(userDir + sp + "src" + sp + "data"));
Process process;
try {
process = processBuilder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
System.out.println("Output is: ");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}