0

I am trying to create a Docker client with the Docker Java API, but I get a ClassNotFoundException when running my code. I am following this Baeldung guide, but I am using the latest version of docker-java-api (version 3.3.3).

Maven import:

<dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java</artifactId>
    <version>3.3.3</version>
</dependency>

Code:

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DockerClientBuilder;

public class Main
{
    public static void main(String[] args)
    {
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();    
    }
}

Full Error:

Connected to the target VM, address: '127.0.0.1:59830', transport: 'socket'
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8.
SLF4J: Ignoring binding found at [jar:file:/Users/idelmas/.m2/repository/org/slf4j/slf4j-jdk14/1.7.36/slf4j-jdk14-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.
Exception in thread "main" java.lang.NoClassDefFoundError: com/github/dockerjava/api/command/LoadImageAsyncCmd
    at com.github.dockerjava.core.DockerClientBuilder.build(DockerClientBuilder.java:101)
    at com.proofpoint.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: com.github.dockerjava.api.command.LoadImageAsyncCmd
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 2 more
Disconnected from the target VM, address: '127.0.0.1:59830', transport: 'socket'

Process finished with exit code 1
blacktide
  • 10,654
  • 8
  • 33
  • 53
Anzu67
  • 23
  • 6
  • 1
    Can you try adding the [`docker-java-api` dependency](https://mvnrepository.com/artifact/com.github.docker-java/docker-java-api/3.3.3) to your `pom.xml` as well and see if that resolves the issue? Just from a quick glance it seems that the `docker-java` dependency doesn't transitively include the api dependency. You may also want to comment on [this GitHub issue](https://github.com/docker-java/docker-java/issues/2022) which seems to be the same problem you're having here. – blacktide Aug 22 '23 at 13:46
  • thanks for pointing out that the docker-java-api was not included with the docker-java. adding this to the pom.xml solved the problem – Anzu67 Aug 22 '23 at 17:45
  • com.github.docker-java docker-java-api 3.3.3 – Anzu67 Aug 22 '23 at 17:45
  • glad it solved the problem. I added the solution as an answer. – blacktide Aug 22 '23 at 23:54

1 Answers1

1

It seems that the docker-java-api dependency is not included with the docker-java dependency.

If you add this dependency to your pom.xml it should resolve the issue:

<dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java-api</artifactId>
    <version>3.3.3</version>
</dependency>
blacktide
  • 10,654
  • 8
  • 33
  • 53