How to run a docker image from java Program?

I’m trying to run an Ubuntu image from a java program using a script; here is how:

my java code:

public static void main(String[] args) {

public static void executeCommand(String filePath) {
    File file = new File(filePath);
    if (!file.isFile()) {
        throw new IllegalArgumentException("The file " + filePath + " does not exist");
    try {
        if (isLinux()) {
            Process p = Runtime.getRuntime().exec("sh " + filePath);
            p.waitFor(); // i tried to remove this but still not work for my me 
        } else if (isWindows()) {
            Runtime.getRuntime().exec("cmd /c start " + filePath);
    } catch (InterruptedException e) {
    } catch (IOException e) {

here is my script file:

sudo docker run ubuntu 

when I go to bin and type:

docker ps


docker ps -a

It should show the running Ubuntu container, but it doesn’t.

Note: there is nothing wrong with the shell location; I try to create file in shell file and it works.

Source: StackOverflow