Docker-java: 'Could not find or load main class' when running jar containing docker-java as dependency

Created on 13 Sep 2018  路  5Comments  路  Source: docker-java/docker-java

Steps to reproduce:

  1. Create basic gradle project with docker-java as a compile dependency
  2. Add a simple HelloWorld main class
  3. Run the gradle jar task
  4. java -jar hello-world.jar

Exception thrown at startup is

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

Removing docker-java from compile dependencies fixes the issue.

Example build.gradle

dependencies {
    compile(
            "com.github.docker-java:docker-java:3.0.14"
    )
}

task helloWorld(type: Jar) {
    manifest {
        attributes 'Main-Class': 'HelloWorld'
    }
    baseName = 'hello-world'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

Reproduced with jdk8, jdk9, jdk10
Tested with docker-java versions 3.0.6, 3.0.14, 3.1.0-rc-4

Most helpful comment

I have exactly the same issue. After including the docker-java as a compile dependency java can't load my main class from the fat jar with a SecurityException "Invalid signature file digest for Manifest main attributes". Actually the same happens when I try to add the docker-client library. The reason was that the library's signature files were copied into the META-INF of my fat jar and it broke class loading of my main class. Here is the fix:

    jar {
        manifest {
            attributes(
                "Main-Class" to "sandbox.MainKt"
            )
        }
        from(configurations.compile.get().map { if (it.isDirectory) it else zipTree(it) })
        from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
        exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
    }

All 5 comments

A workaround we've found for this is to build a skinny jar with the dependencies on the classpath rather than bundling everything into a fat jar.

I have exactly the same issue. After including the docker-java as a compile dependency java can't load my main class from the fat jar with a SecurityException "Invalid signature file digest for Manifest main attributes". Actually the same happens when I try to add the docker-client library. The reason was that the library's signature files were copied into the META-INF of my fat jar and it broke class loading of my main class. Here is the fix:

    jar {
        manifest {
            attributes(
                "Main-Class" to "sandbox.MainKt"
            )
        }
        from(configurations.compile.get().map { if (it.isDirectory) it else zipTree(it) })
        from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
        exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
    }

@sedovalx I'm facing this same exact problem but I'm new to this, so I'm not sure how would I go about using your fix. Could you elaborate a little on how to implement your fix? Thanks in advance

@dwgabriel the code fragment above is from my Gradle build script. It describes the configuration of the jar task that can be run with ./gradlew jar. The task assembles an executable jar file that can be executed with java -jar my-app.jar.

Build scripts in Gradle typically named as build.gradle (if written in Groovy) or build.gradle.kts (in case of Kotlin).

Still broken in 3.2.6, same steps to reproduce, add dep. docker-java, build a fat jar, run with java -jar fat.jar:

dependencies {
    ...
    implementation("com.github.docker-java:docker-java:3.2.6")
}

tasks.withType<Jar> {
    manifest { attributes["Main-Class"] = "com.company.application.Main" }

    configurations["compileClasspath"].forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}

Is an obscure workaround enough to consider this issue closed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noahmonster picture noahmonster  路  10Comments

zysaaa picture zysaaa  路  4Comments

lispyclouds picture lispyclouds  路  3Comments

kpnarayanan picture kpnarayanan  路  4Comments

alecharp picture alecharp  路  3Comments