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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

12reactions
sedovalxcommented, Apr 26, 2019

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")
    }
1reaction
sedovalxcommented, Aug 22, 2019

@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).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java – “Could Not Find or Load Main Class” Error | Baeldung
Occasionally when we run a Java program, we might see “Could not find or load main class.” It's easy to guess the reason:...
Read more >
Could not find or load main class with a Jar File - Stack Overflow
I got it working like this: TestClass.Java package classes; public class TestClass { public static void main(String[] args) { System.out.println("Test"); } }.
Read more >
Java Guide: How to Fix "Could not find or load main class"
The Java “Could not find or load main class” error is thrown when the JVM fails to find or load the main class...
Read more >
Error: Could not find or load main class in Java [Solved]
Error: Could not find or load main class HelloWorld comes when you are trying to run your Java program using java command with...
Read more >
Here's How to Fix the "Could Not Find or Load Main Class ...
The classpath is the file path where the JRE (Java runtime environment) searches for the classes and other resource files to run the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found