1

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) {
    executeCommand("/home/abrahem/IdeaProjects/untitled3/src/createContainer.sh");
}


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) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

here is my createContainer.sh script file:

#!bin/sh
sudo docker run ubuntu 

when I go to bin and type:

docker ps

or

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.

10
  • debug your code in try block. Commented Jan 14, 2020 at 3:46
  • sudo docker run --name myubuntu -itd ubuntu try this instead. Commented Jan 14, 2020 at 3:51
  • @RamPrakash thank for comment, but how? there is no exception to debug and i have try block. Commented Jan 14, 2020 at 3:58
  • @TaraPrasadGurung i tried,but still not work for me Commented Jan 14, 2020 at 4:02
  • 1
    You can try and follow this tutorial: baeldung.com/run-shell-command-in-java to obtain the output from the program you run. Commented Jan 14, 2020 at 8:47

1 Answer 1

1

You do not capture any error messages or normal output from your process. Maybe it just works?

Use getErrorStream() and getOutputStream() methods of Process to capture the output from the process somewhat like described here. You may just see the expected output. If not, it should be the error message on the error stream.

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.