1

I'm building a docker image from a jax-rs web application built with Maven. The project builds an image from the Dockerfile, but when I'm about to run the container I get Error: Could not find or load main class.

I've been looking through the docker image with docker inspect and noticed that the Entrypoint from the Dockerfile is not copied in the right way. I have another project where building the image and running it is working, where the Dockerfile is set up in the same way, and in those case the ENTRYPOINT is copied correctly. So I think this is the problem, but I haven't found I way to solve it.

Edit: After correcting an error in my Dockerfile, I can exclude that the copying of entrypoints to the image is the problem. However, the error remains: I now get the error "Could not find or load main class .opt.cartservicejava-swarm.jar" where before it was only "Could not find or load main class".

This is the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.cartservicejava</groupId>
    <artifactId>cartservicejava</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <finalName>cartservicejava</finalName>
        <plugins>
            <plugin>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>wildfly-swarm-plugin</artifactId>
                <version>${version.wildfly-swarm}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.wildfly-swarm>2017.7.0</version.wildfly-swarm>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly.swarm</groupId>
            <artifactId>jaxrs</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.wildfly.swarm</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.wildfly-swarm}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

This is the Dockerfile

FROM openjdk:jre-alpine

COPY target/cartservicejava-swarm.jar /opt/cartservicejava-swarm.jar

EXPOSE 8080
# preferIPv4Stack is needed to keep wildfly-swarm happy
ENTRYPOINT ["java", "-Djava.net.preferIPv4Stack=true", "/opt/cartservicejava-swarm.jar"]
fofoky
  • 11
  • 1
  • 6
  • This seems to be a problem with the jar itself. Can you run the jar on your machine simply by running `java -jar target/cartservicejava-swarm.jar`? – yamenk Apr 19 '18 at 13:42
  • thanks! yes, I whould've added that to the question. It works to run the jar outside the Dockerfile. – fofoky Apr 19 '18 at 13:45
  • btw the second empty entry in `["java", "", "-jar", "/opt/cartservicejava-swarm.jar"]`is not needed. – yamenk Apr 19 '18 at 13:47
  • thanks @yamenk I removed it now, let's see what that does for the project – fofoky Apr 19 '18 at 13:52
  • The Entrypoint now copies correctly to the image, but now I get the error "Could not find or load main class .opt.cartservicejava-swarm.jar" instead of just "Could not find or load main class". – fofoky Apr 19 '18 at 14:02
  • get a shell inside the container and have a look at the files. – Thorbjørn Ravn Andersen Apr 19 '18 at 21:42
  • Thanks @ThorbjørnRavnAndersen - I couldn't get a shell since the container couldn't run, but I inspected the container and found the error - which was a missed entry in the dockerfile. Silly mistake. Solved, thank you both. This is what the last line in the Dockerfile should look like: ENTRYPOINT ["java", "-Djava.net.preferIPv4Stack=true", "-jar", "/opt/cartservicejava-swarm.jar"] – fofoky Apr 24 '18 at 18:48
  • Sure you can. Just tell docker to execute /bin/sh in the container. – Thorbjørn Ravn Andersen Apr 24 '18 at 19:24
  • thanks a lot @ThorbjørnRavnAndersen! – fofoky Apr 26 '18 at 07:40

0 Answers0