Skip to content
Advertisement

No Main Class found when running a dockerizing image

I have a university project which consist of building a Java Spring Boot application that can handle multiple APIs that can start new docker containers running jar applications.

For the moment, I was able to experiment how Spring Boot works, so I built a simple jar to deploy a simple greeting API that returns “Hello World!”

So I have built and pushed a docker image using jib-core to my DockerHub repo which work fine.

public static void main(String[] args) throws InvalidImageReferenceException, IOException, InterruptedException, ExecutionException, RegistryException, CacheDirectoryCreationException {
    Jib.from("openjdk:15")
            .addLayer(Arrays.asList(Paths.get("apps/demo-0.0.1-SNAPSHOT.jar")), AbsoluteUnixPath.get("/"))
            .setEntrypoint("java", "demo-0.0.1-SNAPSHOT.jar")
            .containerize(
                    Containerizer.to(RegistryImage.named("dmh911/localkube:latest")
                            .addCredential("username", "password")));
}

The problem is, when I execute the following command docker run -i -t dmh911/localkube:latest, I get in return this error:

Error: Could not find or load main class demo-0.0.1-SNAPSHOT.jar
Caused by: java.lang.ClassNotFoundException: demo-0.0.1-SNAPSHOT.jar

This is the jar MANIFEST file:

Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 15
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.restservice.DemoApplication
Spring-Boot-Version: 2.3.4.RELEASE
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx

I know that the problem is probably something very silly, but I don’t know what exactly.

Advertisement

Answer

What you’re code will execute is java demo-0.0.1-SNAPSHOT.jar, which will try to run the class demo-0.0.1-SNAPSHOT.jar, which is unlikely to exist because this is probably the name of your jar file, and not the class you want to run.

Instead your entrypoint should be java -jar demo-0.0.1-SNAPSHOT.jar, which means you need to change your code to .setEntrypoint("java", "-jar", "demo-0.0.1-SNAPSHOT.jar").

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement