Thanks to visit codestin.com
Credit goes to github.com

Skip to content

use a certain output as the input of docker exec... #2306

Closed
@Aurora-up

Description

@Aurora-up

I want to use docker-java to execute like docker exec -i <containerId> java -cp /codeStore Main < ~/input.txt
When I write String[] cmd Args = new String[] { "java", "-cp", "/codeStore", "Main", "<", "~/input.txt"}; like this As a result, the program kept running and could not be stopped, so I used dockerClient.execStartCmd(execCreateCmdResponse.getId()).withStdIn(inputStream)
The writing method of withStdIn(inputStream) is used in , but the following error occurs;
My requirement is to use a certain output as the input of docker exec.... I sincerely ask the author which API can do this.

....
dockerClient.startContainerCmd(containerId).exec();


System.out.println("----------------------------------------------");
String[] cmdArgs = new String[] { "java", "-cp", "/codeStore", "Main"};
var execCreateCmdResponse = dockerClient.execCreateCmd(containerId)
		.withCmd(cmdArgs)
		.withAttachStdin(true)
		.withAttachStdout(true)
		.withAttachStderr(true)
		.exec();
System.out.println("----------------------------------------------");
System.out.println( execCreateCmdResponse.getRawValues());


var execStartResultCallback = new ExecStartResultCallback() {
	@Override
	public void onNext(Frame frame) {
		if (frame.getStreamType() == StreamType.STDERR) {
			System.out.println( new String(frame.getPayload()));
		} else {
			System.out.println(new String(frame.getPayload()));
		}
		super.onNext(frame);
	}
};

String inputString = "2 5\n1 2\n2 4";

byte[] inputBytes = inputString.getBytes(StandardCharsets.UTF_8);

InputStream inputStream = new ByteArrayInputStream(inputBytes);

try {
	dockerClient.execStartCmd(execCreateCmdResponse.getId())
			.withStdIn(inputStream)
			.exec(execStartResultCallback)
			.awaitCompletion();
} catch (InterruptedException e) {
	e.printStackTrace();
}
16:03:08.420 [docker-java-stream-2145430985] ERROR com.github.dockerjava.api.async.ResultCallbackTemplate - Error during callback
java.lang.UnsupportedOperationException: Does not support hijacking
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.execute(JerseyDockerHttpClient.java:288)
        at com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:228)
        at com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
        at java.base/java.lang.Thread.run(Thread.java:840)
Exception in thread "main" java.lang.UnsupportedOperationException: Does not support hijacking
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.execute(JerseyDockerHttpClient.java:288)
        at com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:228)
        at com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
        at java.base/java.lang.Thread.run(Thread.java:840)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions