Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I get input from terminal in a container? #2308

Open
hyb200 opened this issue Feb 11, 2024 · 3 comments
Open

How can I get input from terminal in a container? #2308

hyb200 opened this issue Feb 11, 2024 · 3 comments

Comments

@hyb200
Copy link

hyb200 commented Feb 11, 2024

I want to run an interactive Java program in docker and need input from a terminal. How do I do this? The Java code is here:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine(); 
        System.out.println(input); 
    }
}
@white2q
Copy link

white2q commented Mar 11, 2024

请问你现在解决这个问题了吗?如何解决的呢?

@YaleXin
Copy link

YaleXin commented May 15, 2024

I have a solution, but it's not perfect, just use "echo" command.
I have Main.java to run:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = Integer.parseInt(scan.next());
        int b = Integer.parseInt(scan.next());
        System.out.println((a + b));
    }
}

and

private List<ExecuteMessage> runCode(String containerId, String language, List<String> inputList) {
        List<ExecuteMessage> executeMessageArrayList = new ArrayList<>();
        SandboxEntry.DockerInfo dockerInfoByName = sandboxEntry.getDockerInfoByName(language);
        String runCmd = dockerInfoByName.getRun();
        for (String inputStr :
                inputList) {
            String curRunCmd = String.format("echo \"%s\" | %s", inputStr, runCmd);
            ExecuteMessage executeMessage = runCmdWithDocker(containerId, curRunCmd);
            executeMessageArrayList.add(executeMessage);
        }
        return executeMessageArrayList;
    }
.....
....
...
private ExecuteMessage runCmdWithDocker(String containerId, String cmd) {
        String[] userCodeCmd = {"sh", "-c", String.format("%s", cmd)};
        log.info("docker cmd = {}", Arrays.stream(userCodeCmd)
                .collect(Collectors.joining(" ")));
        final boolean[] timeout = {true};
        ExecuteMessage executeMessage = new ExecuteMessage();
        ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
                .withAttachStdout(true)
                .withAttachStderr(true)
                .withAttachStdin(true)
                .withCmd(userCodeCmd)
                .exec();
......
.....
}

finally, terminal will output:

docker cmd = sh -c echo "1 2" | java Main

You can get more detail from my code repository

@YaleXin
Copy link

YaleXin commented May 16, 2024

Oh, you can get better solution from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants